diff --git a/backend/database.js b/backend/database.js index 1e74cf7..57e6918 100644 --- a/backend/database.js +++ b/backend/database.js @@ -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) { diff --git a/backend/routes.js b/backend/routes.js index 304b8f4..4369b9d 100644 --- a/backend/routes.js +++ b/backend/routes.js @@ -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(); @@ -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)); }