Persist Google picture and sub on login via setProfile.

Store profile fields from the verified ID token and read picture from the DB on authenticated requests.
This commit is contained in:
seb
2026-08-05 11:04:44 +02:00
parent 782c49a680
commit c78237a1dd
3 changed files with 13 additions and 1 deletions

View File

@@ -76,7 +76,7 @@ export const authMiddleware = async (req, res, next) => {
id: user.id, id: user.id,
email: user.email, email: user.email,
selectedCompanyId: user.selectedCompanyId, selectedCompanyId: user.selectedCompanyId,
...(decoded.picture && { picture: decoded.picture }), ...(user.picture && { picture: user.picture }),
}; };
const now = Math.floor(Date.now() / 1000); const now = Math.floor(Date.now() / 1000);

View File

@@ -2,6 +2,7 @@ import jwt from 'jsonwebtoken';
import { OAuth2Client } from 'google-auth-library'; import { OAuth2Client } from 'google-auth-library';
import env from '../../../config/env.js'; import env from '../../../config/env.js';
import * as db from '../../../db/googleAuth.js'; import * as db from '../../../db/googleAuth.js';
import { clearCachedUser } from '../../middleware/auth.js';
const googleClient = new OAuth2Client(env.googleClientId); const googleClient = new OAuth2Client(env.googleClientId);
@@ -108,6 +109,12 @@ export function register(app, basePath = '') {
return res.status(403).json({ error: 'Email is not authorized' }); return res.status(403).json({ error: 'Email is not authorized' });
} }
await db.setProfile(allowed.id, {
picture: payload.picture ?? null,
sub: payload.sub ?? null,
});
clearCachedUser(email);
const user = { const user = {
id: allowed.id, id: allowed.id,
email: allowed.email, email: allowed.email,

View File

@@ -31,3 +31,8 @@ export async function setSelectedCompany(authId, companyId) {
const store = await getStore(); const store = await getStore();
await store.google_auth.setSelectedCompany(authId, companyId); await store.google_auth.setSelectedCompany(authId, companyId);
} }
export async function setProfile(authId, { picture = null, sub = null } = {}) {
const store = await getStore();
await store.google_auth.setProfile(authId, { picture, sub });
}