From 0be0f55fd20bc39072331b1996d315add31df331 Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Tue, 26 May 2026 09:38:48 +0200 Subject: [PATCH] feat(auth): bump users.token_version on admin permission changes (2/3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- src/app/api/admin/users/[id]/permissions/route.ts | 8 ++++++++ src/app/api/admin/users/[id]/route.ts | 14 +++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/app/api/admin/users/[id]/permissions/route.ts b/src/app/api/admin/users/[id]/permissions/route.ts index 259fac0cf..a24e82c79 100644 --- a/src/app/api/admin/users/[id]/permissions/route.ts +++ b/src/app/api/admin/users/[id]/permissions/route.ts @@ -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' }) diff --git a/src/app/api/admin/users/[id]/route.ts b/src/app/api/admin/users/[id]/route.ts index 70a6cc5a4..14f962f26 100644 --- a/src/app/api/admin/users/[id]/route.ts +++ b/src/app/api/admin/users/[id]/route.ts @@ -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 = {} if (phone !== undefined) profileUpdate.phone = phone