@@ -143,9 +143,18 @@ function getMicrosoftUserInfoFromIdToken(tokens: { accessToken?: string }, provi
143143}
144144
145145const blockedSignupDomains = env . BLOCKED_SIGNUP_DOMAINS
146- ? new Set ( env . BLOCKED_SIGNUP_DOMAINS . split ( ',' ) . map ( ( d ) => d . trim ( ) . toLowerCase ( ) ) )
146+ ? env . BLOCKED_SIGNUP_DOMAINS . split ( ',' )
147+ . map ( ( d ) => d . trim ( ) . toLowerCase ( ) )
148+ . filter ( Boolean )
147149 : null
148150
151+ function isSignupEmailBlocked ( email : string | undefined | null ) : boolean {
152+ if ( ! blockedSignupDomains || ! email ) return false
153+ const domain = email . split ( '@' ) [ 1 ] ?. toLowerCase ( )
154+ if ( ! domain ) return false
155+ return blockedSignupDomains . some ( ( entry ) => domain === entry || domain . endsWith ( `.${ entry } ` ) )
156+ }
157+
149158const additionalTrustedOrigins = parseOriginList ( env . TRUSTED_ORIGINS , ( value ) =>
150159 logger . warn ( 'Ignoring invalid entry in TRUSTED_ORIGINS' , { value } )
151160)
@@ -219,11 +228,8 @@ export const auth = betterAuth({
219228 user : {
220229 create : {
221230 before : async ( user ) => {
222- if ( blockedSignupDomains ) {
223- const emailDomain = user . email ?. split ( '@' ) [ 1 ] ?. toLowerCase ( )
224- if ( emailDomain && blockedSignupDomains . has ( emailDomain ) ) {
225- throw new Error ( 'Sign-ups from this email domain are not allowed.' )
226- }
231+ if ( isSignupEmailBlocked ( user . email ) ) {
232+ throw new Error ( 'Sign-ups from this email domain are not allowed.' )
227233 }
228234 return { data : user }
229235 } ,
@@ -814,14 +820,8 @@ export const auth = betterAuth({
814820 }
815821 }
816822
817- if ( ctx . path . startsWith ( '/sign-up' ) && blockedSignupDomains ) {
818- const requestEmail = ctx . body ?. email ?. toLowerCase ( )
819- if ( requestEmail ) {
820- const emailDomain = requestEmail . split ( '@' ) [ 1 ]
821- if ( emailDomain && blockedSignupDomains . has ( emailDomain ) ) {
822- throw new Error ( 'Sign-ups from this email domain are not allowed.' )
823- }
824- }
823+ if ( ctx . path . startsWith ( '/sign-up' ) && isSignupEmailBlocked ( ctx . body ?. email ) ) {
824+ throw new Error ( 'Sign-ups from this email domain are not allowed.' )
825825 }
826826
827827 if ( ctx . path === '/oauth2/authorize' || ctx . path === '/oauth2/token' ) {
0 commit comments