Skip to content

Commit

Permalink
Fix sign up rate-limiting issues (#7344)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjeffers authored Jan 25, 2024
1 parent 1bcc737 commit 0513090
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
31 changes: 24 additions & 7 deletions packages/common/src/schemas/sign-on/emailSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { EMAIL_REGEX } from 'utils/email'
export const emailSchemaMessages = {
emailRequired: 'Please enter an email.',
invalidEmail: 'Please enter a valid email.',
emailInUse: 'Email already taken.'
emailInUse: 'Email already taken.',
somethingWentWrong: 'Something went wrong. Try again later.'
}

export const emailSchema = <T extends AudiusQueryContextType>(
Expand All @@ -17,10 +18,26 @@ export const emailSchema = <T extends AudiusQueryContextType>(
email: z
.string({ required_error: emailSchemaMessages.emailRequired })
.regex(EMAIL_REGEX, { message: emailSchemaMessages.invalidEmail })
.refine(
async (email) => {
return !(await signUpFetch.isEmailInUse({ email }, queryContext))
},
{ message: emailSchemaMessages.emailInUse }
)
.superRefine(async (email, ctx) => {
const validEmail = EMAIL_REGEX.test(email)
if (!validEmail) return true
const isEmailInUse = await signUpFetch.isEmailInUse(
{ email },
queryContext
)
if (isEmailInUse === undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: emailSchemaMessages.somethingWentWrong
})
return true
} else if (isEmailInUse) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: emailSchemaMessages.emailInUse
})
return true
}
return false
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const NewEmailField = (props: NewEmailFieldProps) => {
{...props}
error={emailInUse ? false : undefined}
helperText={emailInUse ? false : undefined}
debouncedValidationMs={500}
/>
{emailInUse || (hadError && isValidating) ? (
<EmailInUseHint onChangeScreen={onChangeScreen} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const NewEmailField = () => {
return (
<>
<EmailField
debouncedValidationMs={500}
// Don't show red error message when emailInUse
helperText={emailInUse ? '' : undefined}
/>
Expand Down

0 comments on commit 0513090

Please sign in to comment.