From 23b251be7c70a0fcddea5ab2f47af3f2146fabbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Tonh=C3=A4user?= Date: Tue, 11 Aug 2026 01:41:18 +0200 Subject: [PATCH] fix(auth): declare role as additionalField so sessions include it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Better Auth's parseUserOutput filters the session user to declared fields only (filterOutputFields over getFields(options,'user','output')). The drizzle adapter selects the custom `role` column, but because it was never declared in the Better Auth config, parseUserOutput stripped it — session.user.role was always undefined. That broke two things: the root redirect fell through to /redaktion/alben for admins (role !== 'ADMIN'), and requireAdmin threw ADMIN_REQUIRED on every /admin page (role !== 'ADMIN'). Declare role via user.additionalFields (the documented custom-field API; the `fields` map only renames core columns). input:false keeps it unsettable on signUp — the DB default and the admin seed own it — and defaultValue matches the column default so inserts never send null into the NOT NULL portalRole enum. Co-Authored-By: Claude --- src/modules/auth/auth.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/modules/auth/auth.ts b/src/modules/auth/auth.ts index b7f103c..1855731 100644 --- a/src/modules/auth/auth.ts +++ b/src/modules/auth/auth.ts @@ -34,6 +34,24 @@ export function createPortalAuth({ // for type uuid". Generate real UUIDs so inserts (signUp, sessions, // verifications) match the schema. advanced: { database: { generateId: () => crypto.randomUUID() } }, + // `role` is a custom column on `users` (portalRole enum: ADMIN | EDITORIAL). + // Better Auth's parseUserOutput filters the session user to declared fields + // only, so without this the drizzle adapter selects `role` but it is then + // stripped from session.user — requireAdmin and the root redirect saw + // role === undefined and misrouted/blocked every admin. Declaring it makes + // Better Auth return it; input:false keeps it unsettable on signUp (the DB + // default / admin seed owns it), defaultValue matches the column default so + // inserts never send null into the NOT NULL enum column. + user: { + additionalFields: { + role: { + type: "string", + required: false, + defaultValue: "EDITORIAL", + input: false, + }, + }, + }, emailAndPassword: { enabled: true, requireEmailVerification: true,