diff --git a/src/accounts/openid.js b/src/accounts/openid.js index f33c7f23f..6693f78f6 100644 --- a/src/accounts/openid.js +++ b/src/accounts/openid.js @@ -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')); @@ -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, }); @@ -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) {