Auth: refresh JWT on verify so permission changes take effect without re-login

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-02 21:07:09 +00:00
parent 6e5009b57f
commit 608ed4ce19

View file

@ -186,6 +186,38 @@ export async function authRoutes(app) {
? allCaps.filter(c => c.startsWith(`${appSlug}:`)).map(c => c.slice(appSlug.length + 1))
: allCaps
// Refresh the JWT cookie with current DB state so downstream app backends
// (which read caps from the JWT) see the updated permissions immediately —
// without requiring the user to log out and back in.
const { rows: apps } = user.is_admin
? await pool.query(
`SELECT slug FROM apps WHERE active = true`
)
: await pool.query(
`SELECT DISTINCT a.slug FROM apps a
WHERE a.active = true
AND (
EXISTS (SELECT 1 FROM user_app_perms p WHERE p.user_id = $1 AND p.app_id = a.id)
OR EXISTS (
SELECT 1 FROM user_roles ur
JOIN role_app_perms rap ON rap.role_id = ur.role_id
WHERE ur.user_id = $1 AND rap.app_id = a.id
)
)`,
[user.id]
)
const freshToken = await signToken({
sub: user.email,
name: user.name,
user_id: user.id,
is_admin: user.is_admin,
offsite_allowed: user.offsite_allowed,
apps: apps.map(a => a.slug),
caps: allCaps,
})
reply.setCookie('hnf_session', freshToken, cookieOpts(request))
return {
user_id: user.id,
email: user.email,