@@ -38,56 +38,52 @@ export const sendOtpAction = actionClient
3838 throw new Error ( "Too many requests. Please try again later." ) ;
3939 }
4040
41- if ( email . includes ( "+" ) && isGenericEmail ( email ) ) {
42- throw new Error (
43- "Email addresses with + are not allowed. Please use your work email instead." ,
44- ) ;
45- }
41+ const isGenericEmailWithPlus = email . includes ( "+" ) && isGenericEmail ( email ) ;
4642
47- const domain = email . split ( "@" ) [ 1 ] ;
43+ const emailDomain = email . split ( "@" ) [ 1 ] ;
4844
49- if ( process . env . NEXT_PUBLIC_IS_DUB ) {
50- const [ isDisposable , emailDomainTerms ] = await Promise . all ( [
51- redis . sismember ( "disposableEmailDomains" , domain ) ,
52- process . env . EDGE_CONFIG ? get ( "emailDomainTerms" ) : [ ] ,
53- ] ) ;
45+ const [ isDisposable , emailDomainTerms ] = await Promise . all ( [
46+ redis . sismember ( "disposableEmailDomains" , emailDomain ) ,
47+ process . env . EDGE_CONFIG ? get ( "emailDomainTerms" ) : [ ] ,
48+ ] ) ;
5449
55- // Only build the regex if we have at least one term; otherwise set to null
56- const blacklistedEmailDomainTermsRegex =
57- emailDomainTerms && Array . isArray ( emailDomainTerms )
58- ? new RegExp (
59- emailDomainTerms
60- . map ( ( term : string ) =>
61- term . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ,
62- ) // replace special characters with escape sequences
63- . join ( "|" ) ,
64- )
65- : null ;
66-
67- if (
68- isDisposable ||
69- ( blacklistedEmailDomainTermsRegex &&
70- blacklistedEmailDomainTermsRegex . test ( domain ) )
71- ) {
72- // edge case: the user already has a partner account on Dub with this email address,
73- // or they have an existing application for a program, we can allow them to continue
74- const [ isPartnerAccount , hasExistingApplications ] = await Promise . all ( [
75- prisma . partner . findUnique ( {
76- where : {
77- email,
78- } ,
79- } ) ,
80- prisma . programApplication . findFirst ( {
81- where : {
82- email,
83- } ,
84- } ) ,
85- ] ) ;
86- if ( ! isPartnerAccount && ! hasExistingApplications ) {
87- throw new Error (
88- "Invalid email address – please use your work email instead. If you think this is a mistake, please contact us at dub.co/support" ,
89- ) ;
90- }
50+ // Only build the regex if we have at least one term; otherwise set to null
51+ const blacklistedEmailDomainTermsRegex =
52+ emailDomainTerms && Array . isArray ( emailDomainTerms )
53+ ? new RegExp (
54+ emailDomainTerms
55+ . map ( ( term : string ) =>
56+ term . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ,
57+ ) // replace special characters with escape sequences
58+ . join ( "|" ) ,
59+ )
60+ : null ;
61+
62+ // if any of the flags match, run one final edge case check, before throwing an error
63+ if (
64+ isGenericEmailWithPlus ||
65+ isDisposable ||
66+ ( blacklistedEmailDomainTermsRegex &&
67+ blacklistedEmailDomainTermsRegex . test ( emailDomain ) )
68+ ) {
69+ // edge case: the user already has a partner account on Dub with this email address,
70+ // or they have an existing application for a program, we can allow them to continue
71+ const [ isPartnerAccount , hasExistingApplications ] = await Promise . all ( [
72+ prisma . partner . findUnique ( {
73+ where : {
74+ email,
75+ } ,
76+ } ) ,
77+ prisma . programApplication . findFirst ( {
78+ where : {
79+ email,
80+ } ,
81+ } ) ,
82+ ] ) ;
83+ if ( ! isPartnerAccount && ! hasExistingApplications ) {
84+ throw new Error (
85+ "Invalid email address – please use your work email instead. If you think this is a mistake, please contact us at dub.co/support" ,
86+ ) ;
9187 }
9288 }
9389
0 commit comments