@@ -16,16 +16,16 @@ const env = envModule?.env ?? (process.env as Record<string, string | undefined>
1616const getRequestEvent = appServer ?. getRequestEvent ?? ( ( ) => undefined as unknown as RequestEvent ) ;
1717const log = loggingModule ?. logger . child ( { module : 'auth' } ) ;
1818
19- // The username is the Trino user we impersonate, so only the OIDC callback may
19+ // trinoUser is the Trino user we impersonate, so only the OIDC callback may
2020// write it. Marking the field `input: false` would also block that: better-auth
2121// drops such fields from the provider profile on sign-up (see #371). Database
2222// hooks see the endpoint's route template, not the request path.
23- const usernameWritePath = '/oauth2/callback/:providerId' ;
23+ const trinoUserWritePath = '/oauth2/callback/:providerId' ;
2424
25- function guardUsernameWrite ( data : Record < string , unknown > , context : { path ?: string } | null ) {
26- if ( ! ( 'username ' in data ) || context ?. path === usernameWritePath ) return ;
27- log ?. warn ( { path : context ?. path } , 'Rejected username write outside the OIDC callback' ) ;
28- throw new APIError ( 'FORBIDDEN' , { message : 'username is not allowed to be set' } ) ;
25+ function guardTrinoUserWrite ( data : Record < string , unknown > , context : { path ?: string } | null ) {
26+ if ( ! ( 'trinoUser ' in data ) || context ?. path === trinoUserWritePath ) return ;
27+ log ?. warn ( { path : context ?. path } , 'Rejected trinoUser write outside the OIDC callback' ) ;
28+ throw new APIError ( 'FORBIDDEN' , { message : 'trinoUser is not allowed to be set' } ) ;
2929}
3030
3131const trinoUserClaim = env . STACKABLE_COCKPIT_TRINO_USER_CLAIM ?? 'preferred_username' ;
@@ -68,16 +68,16 @@ export const auth = betterAuth({
6868 } ,
6969 user : {
7070 additionalFields : {
71- username : {
71+ trinoUser : {
7272 type : 'string' ,
7373 required : false
7474 }
7575 }
7676 } ,
7777 databaseHooks : {
7878 user : {
79- create : { before : async ( user , context ) => guardUsernameWrite ( user , context ) } ,
80- update : { before : async ( user , context ) => guardUsernameWrite ( user , context ) }
79+ create : { before : async ( user , context ) => guardTrinoUserWrite ( user , context ) } ,
80+ update : { before : async ( user , context ) => guardTrinoUserWrite ( user , context ) }
8181 }
8282 } ,
8383 plugins : [
@@ -101,8 +101,8 @@ export const auth = betterAuth({
101101 const fullName = [ profile . given_name , profile . family_name ]
102102 . filter ( Boolean )
103103 . join ( ' ' ) ;
104- const trinoUser = profile [ trinoUserClaim ] ;
105- if ( trinoUser ) {
104+ const claimedTrinoUser = profile [ trinoUserClaim ] ;
105+ if ( claimedTrinoUser ) {
106106 log ?. debug (
107107 { trino_user_claim : trinoUserClaim } ,
108108 'Resolved Trino user from OIDC claim'
@@ -119,8 +119,8 @@ export const auth = betterAuth({
119119 emailVerified : profile . email_verified ?? false ,
120120 image : profile . picture || null ,
121121 // No `|| ''`: without an email better-auth rejects the sign-in before
122- // creating the user, so this never stores an empty username .
123- username : trinoUser || profile . preferred_username || profile . email
122+ // creating the user, so this never stores an empty Trino user .
123+ trinoUser : claimedTrinoUser || profile . preferred_username || profile . email
124124 } ;
125125 }
126126 }
0 commit comments