Skip to content

Commit

Permalink
Update profile email on oidc auth if changed
Browse files Browse the repository at this point in the history
  • Loading branch information
nebulade committed Jun 5, 2024
1 parent ab23516 commit 9889e3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 6 additions & 2 deletions backend/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ async function usersGet(userId) {
return result[0];
}

async function usersUpdate(userId, githubToken) {
async function usersUpdate(userId, githubToken, email) {
assert.strictEqual(typeof userId, 'string');
assert.strictEqual(typeof githubToken, 'string');
assert.strictEqual(typeof email, 'string');

await db.query('UPDATE users SET githubToken=? WHERE id=?', [ githubToken, userId ]);
let args = [ githubToken, email, userId ];
let query = 'UPDATE users SET githubToken=?,email=? WHERE id=?';

await db.query(query, args);
}

async function releasesList(projectId) {
Expand Down
11 changes: 10 additions & 1 deletion backend/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ async function auth(req, res, next) {
}
}

// update email if changed
if (user.email !== req.oidc.user.email) {
try {
await database.users.update(user.id, user.githubToken, user.email);
} catch (e) {
console.error('Failed to update email for user.', user, e);
}
}

req.user = user;

next();
Expand All @@ -81,7 +90,7 @@ async function profileUpdate(req, res, next) {
}

try {
await database.users.update(req.user.id, githubToken);
await database.users.update(req.user.id, githubToken, req.user.email);
} catch (error) {
return next(new HttpError(500, error));
}
Expand Down

0 comments on commit 9889e3b

Please sign in to comment.