@@ -32,6 +32,9 @@ export const configuredProviders = socialProviderList.reduce<
3232 requireSelectAccount ?: boolean ;
3333 clientKey ?: string ;
3434 issuer ?: string ;
35+ // Google-specific optional hints
36+ hostedDomain ?: string ;
37+ hd ?: string ;
3538 }
3639 >
3740> ( ( acc , provider ) => {
@@ -69,6 +72,22 @@ export const configuredProviders = socialProviderList.reduce<
6972 acc [ provider ] . tenantId = "common" ;
7073 acc [ provider ] . requireSelectAccount = true ;
7174 }
75+ // Add Google domain hint if allowed domains is configured
76+ if (
77+ provider === "google" &&
78+ Object . keys ( acc ) . includes ( "google" ) &&
79+ acc [ provider ]
80+ ) {
81+ const allowed = process . env . BETTER_AUTH_ALLOWED_DOMAINS
82+ ?. split ( "," )
83+ . map ( ( d ) => d . trim ( ) . toLowerCase ( ) )
84+ . filter ( Boolean ) ;
85+ if ( allowed && allowed . length > 0 ) {
86+ // Use the first domain as an authorization hint
87+ acc [ provider ] . hostedDomain = allowed [ 0 ] ;
88+ acc [ provider ] . hd = allowed [ 0 ] ;
89+ }
90+ }
7291 if (
7392 provider === "tiktok" &&
7493 Object . keys ( acc ) . includes ( "tiktok" ) &&
@@ -343,7 +362,18 @@ export const initAuth = (db: dbClient) => {
343362 return Promise . resolve ( false ) ;
344363 }
345364
346- return Promise . resolve ( true ) ;
365+ // Fall through to any additional checks below
366+ }
367+ // Enforce allowed domains (OIDC/social) if configured
368+ const allowed = process . env . BETTER_AUTH_ALLOWED_DOMAINS
369+ ?. split ( "," )
370+ . map ( ( d ) => d . trim ( ) . toLowerCase ( ) )
371+ . filter ( Boolean ) ;
372+ if ( allowed && allowed . length > 0 ) {
373+ const domain = user . email . split ( "@" ) [ 1 ] ?. toLowerCase ( ) ;
374+ if ( ! domain || ! allowed . includes ( domain ) ) {
375+ return Promise . resolve ( false ) ;
376+ }
347377 }
348378 return Promise . resolve ( true ) ;
349379 } ,
0 commit comments