fix(auth): declare role as additionalField so sessions include it
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
7cb168e846
commit
23b251be7c
1 file changed
+18
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user