Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
lelemm and coderabbitai[bot] authored Nov 11, 2024
1 parent 83a4033 commit d71f81f
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/accounts/openid.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ export async function bootstrapOpenId(config) {
}

let accountDb = getAccountDb();
accountDb.transaction(() => {
accountDb.mutate('DELETE FROM auth WHERE method = ?', ['openid']);
accountDb.mutate('UPDATE auth SET active = 0');
accountDb.mutate(
"INSERT INTO auth (method, display_name, extra_data, active) VALUES ('openid', 'OpenID', ?, 1)",
[JSON.stringify(config)],
);

console.log(accountDb.all('select * from auth'));
});
try {
accountDb.transaction(() => {
accountDb.mutate('DELETE FROM auth WHERE method = ?', ['openid']);
accountDb.mutate('UPDATE auth SET active = 0');
accountDb.mutate(
"INSERT INTO auth (method, display_name, extra_data, active) VALUES ('openid', 'OpenID', ?, 1)",
[JSON.stringify(config)],
);
});
} catch (err) {
console.error('Error updating auth table:', err);
return { error: 'database-error' };
}

console.log(accountDb.all('select * from auth'));

Expand All @@ -60,7 +63,7 @@ async function setupOpenIdClient(config) {
const client = new issuer.Client({
client_id: config.client_id,
client_secret: config.client_secret,
redirect_uri: config.server_hostname + '/openid/callback',
redirect_uri: new URL('/openid/callback', config.server_hostname).toString(),
validate_id_token: true,
});

Expand Down Expand Up @@ -180,22 +183,25 @@ export async function loginWithOpenIdFinalize(body) {
return { error: 'openid-grant-failed: no identification was found' };
}

let { countUsersWithUserName } = accountDb.first(
'SELECT count(*) as countUsersWithUserName FROM users WHERE user_name <> ?',
[''],
);
let userId = null;
if (countUsersWithUserName === 0) {
userId = uuid.v4();
accountDb.mutate(
'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, 1, ?)',
[
userId,
identity,
userInfo.name ?? userInfo.email ?? identity,
'ADMIN',
],
accountDb.transaction(() => {
let { countUsersWithUserName } = accountDb.first(
'SELECT count(*) as countUsersWithUserName FROM users WHERE user_name <> ?',
[''],
);
if (countUsersWithUserName === 0) {
userId = uuid.v4();
accountDb.mutate(
'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, 1, ?)',
[
userId,
identity,
userInfo.name ?? userInfo.email ?? identity,
'ADMIN',
],
);
}
});

const userFromPasswordMethod = getUserByUsername('');
if (userFromPasswordMethod) {
Expand Down

0 comments on commit d71f81f

Please sign in to comment.