mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-04 04:06:59 +01:00
feat(auth): bump users.token_version on admin permission changes (2/3)
Builds on ae52f739 (schema + migration for token_version). Two admin
routes now increment users.token_version when they change a JWT-
relevant field:
- PATCH /api/admin/users/[id]: bumps when is_staff changes
- PATCH /api/admin/users/[id]/permissions: bumps on every call
(always touches staff_permissions or is_super_admin)
The bump is sql`token_version + 1` inside the existing UPDATE so
it's atomic with the permission change — no race window where
permissions are updated but tokenVersion isn't.
What's not bumping (intentional):
- Cosmetic field changes (name, email, phone, address) — the
JWT doesn't store these, so the existing token's claims are
still correct. Avoiding spurious bumps keeps the perf cost of
the commit-3 DB lookup on token refresh near zero in steady
state.
- Member-related fields (is_member, member_since, member_type)
aren't currently in the JWT, so no bump needed.
Standalone effect of this commit: token_version increments are
recorded but nothing observes them yet — Auth.js still uses the
existing stale-token semantics. Commit 3/3 wires the jwt callback
to compare token.tokenVersion against users.token_version and
re-fetch permissions on divergence.
28 admin-users tests pass across 3 suites (unchanged — none
exercise the tokenVersion field directly; the integration regression
will land with commit 3/3's tests). tsc clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ae52f73960
commit
0be0f55fd2
2 files changed
+21
-1
No files matched your search
@@ -130,6 +130,14 @@ export const PATCH = withAdmin<{ id: string }>('users', async (request, session,
|
||||
return apiBadRequest('Keine Aktualisierungen angegeben')
|
||||
}
|
||||
|
||||
// Bump tokenVersion so the Auth.js jwt callback re-fetches
|
||||
// permissions on the user's next token refresh. Without this, a
|
||||
// demoted user keeps their old token's staff_permissions /
|
||||
// isSuperAdmin claims until the 30-day maxAge expires or they
|
||||
// manually re-login. See commit 3/3 of the JWT-stale-permissions
|
||||
// sequence for the callback enforcement side.
|
||||
update.tokenVersion = sql`${users.tokenVersion} + 1`
|
||||
|
||||
await db.update(users).set(update).where(eq(users.id, id))
|
||||
|
||||
return apiSuccess({ message: 'Benutzerberechtigungen erfolgreich aktualisiert' })
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { db } from '@/db'
|
||||
import { users, sessions, accounts, userProfiles } from '@/db/schema'
|
||||
import { eq, and, ne } from 'drizzle-orm'
|
||||
import { eq, and, ne, sql } from 'drizzle-orm'
|
||||
import { withAdmin } from '@/lib/api/middleware'
|
||||
import { isSuperAdmin, SUPER_ADMIN_EMAILS } from '@/lib/permissions'
|
||||
import { logger } from '@/lib/logger'
|
||||
@@ -102,6 +102,18 @@ export const PATCH = withAdmin<{ id: string }>('users', async (request, session,
|
||||
if (email !== undefined) userUpdate.email = email
|
||||
if (is_staff !== undefined) userUpdate.isStaff = is_staff
|
||||
|
||||
// Bump tokenVersion if a JWT-relevant field changed. Forces the
|
||||
// Auth.js jwt callback to re-fetch permissions on the user's next
|
||||
// token refresh (see commit 3/3 of the JWT-stale-permissions
|
||||
// sequence). Without this, demoting a user via `is_staff: false`
|
||||
// leaves their existing token claiming isStaff=true until the
|
||||
// 30-day maxAge expires or they manually re-login. The bump is
|
||||
// a no-op for cosmetic changes (name, email, phone, address)
|
||||
// because the token doesn't store those.
|
||||
if (is_staff !== undefined) {
|
||||
userUpdate.tokenVersion = sql`${users.tokenVersion} + 1`
|
||||
}
|
||||
|
||||
// Build profile table update (phone and address live on user_profiles)
|
||||
const profileUpdate: Record<string, unknown> = {}
|
||||
if (phone !== undefined) profileUpdate.phone = phone
|
||||
|
||||
Reference in new issue
Block a user