Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 134
⚠️ [PAY-2387][PAY-2385] Web/Mobile Web: Change Email and Change Password#7352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
08d34e9ec0b4393fb1f82a0a7f103825505bf3a89a752fbf4a1e100d7321d61452a1fa9daaca961f9e498bca286ff5ea6aed129f34736bb558ad42066d898ac5a1191c07f156c7e7357c817eaae1b2036101c9eabba78a2cbe8e079e81275c87c110d63130789979d297598f4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { useState, useCallback, useMemo } from 'react' | ||
| import { FormikHelpers } from 'formik' | ||
| import { z } from 'zod' | ||
| import { toFormikValidationSchema } from 'zod-formik-adapter' | ||
| import { useAudiusQueryContext } from '~/audius-query' | ||
| import { useAppContext } from '~/context' | ||
| import { confirmEmailSchema, emailSchema } from '~/schemas' | ||
| import { isOtpMissingError } from './useChangePasswordFormConfiguration' | ||
| const messages = { | ||
| invalidCredentials: 'Invalid credentials.', | ||
| somethingWrong: 'Something went wrong.', | ||
| passwordRequired: 'Please enter a password.', | ||
| emailUpdated: 'Email updated!' | ||
| } | ||
| export enum ChangeEmailPage { | ||
| ConfirmPassword = 0, | ||
| NewEmail = 1, | ||
| VerifyEmail = 2 | ||
| } | ||
| export type ChangeEmailFormValues = { | ||
| email: string | ||
| oldEmail: string | ||
| password: string | ||
| otp: string | ||
| } | ||
| const initialValues: ChangeEmailFormValues = { | ||
| email: '', | ||
| oldEmail: '', | ||
| password: '', | ||
| otp: '' | ||
| } | ||
| const confirmPasswordFormikSchema = toFormikValidationSchema( | ||
| z.object({ | ||
| password: z.string({ | ||
| required_error: messages.passwordRequired | ||
| }) | ||
| }) | ||
| ) | ||
| const verifyEmailFormikSchema = toFormikValidationSchema(confirmEmailSchema) | ||
| export const useChangeEmailFormConfiguration = (onComplete: () => void) => { | ||
| const { audiusBackend } = useAppContext() | ||
| const [page, setPage] = useState(ChangeEmailPage.ConfirmPassword) | ||
| const audiusQueryContext = useAudiusQueryContext() | ||
| const EmailSchema = useMemo( | ||
| () => toFormikValidationSchema(emailSchema(audiusQueryContext)), | ||
| [audiusQueryContext] | ||
| ) | ||
| const validationSchema = | ||
| page === ChangeEmailPage.ConfirmPassword | ||
| ? confirmPasswordFormikSchema | ||
| : page === ChangeEmailPage.NewEmail | ||
| ? EmailSchema | ||
| : page === ChangeEmailPage.VerifyEmail | ||
| ? verifyEmailFormikSchema | ||
| : undefined | ||
| const checkPassword = useCallback( | ||
| async ( | ||
| values: ChangeEmailFormValues, | ||
| helpers: FormikHelpers<ChangeEmailFormValues> | ||
| ) => { | ||
| const { oldEmail, password } = values | ||
| const libs = await audiusBackend.getAudiusLibsTyped() | ||
| try { | ||
| const confirmed = await libs.Account?.confirmCredentials({ | ||
| username: oldEmail, | ||
| password, | ||
| softCheck: true | ||
| }) | ||
| if (confirmed) { | ||
| helpers.setFieldTouched('email', false) | ||
| setPage(ChangeEmailPage.NewEmail) | ||
| } else { | ||
| helpers.setFieldError('password', messages.invalidCredentials) | ||
| } | ||
| } catch (e) { | ||
| helpers.setFieldError('password', messages.invalidCredentials) | ||
| } | ||
| }, | ||
| [setPage, audiusBackend] | ||
| ) | ||
| const changeEmail = useCallback( | ||
| async ( | ||
| values: ChangeEmailFormValues, | ||
| helpers: FormikHelpers<ChangeEmailFormValues> | ||
| ) => { | ||
| const { oldEmail, password, email, otp } = values | ||
| const sanitizedOtp = otp.replace(/\s/g, '') | ||
| const libs = await audiusBackend.getAudiusLibsTyped() | ||
| try { | ||
| // Try to change email | ||
| await libs.identityService!.changeEmail({ | ||
| email, | ||
| otp: sanitizedOtp | ||
| }) | ||
| await libs.Account!.changeCredentials({ | ||
| newUsername: email, | ||
| newPassword: password, | ||
| oldUsername: oldEmail, | ||
| oldPassword: password | ||
| }) | ||
| onComplete() | ||
| } catch (e) { | ||
| if (isOtpMissingError(e)) { | ||
| helpers.setFieldTouched('otp', false) | ||
| setPage(ChangeEmailPage.VerifyEmail) | ||
| } else { | ||
| helpers.setFieldError('otp', messages.invalidCredentials) | ||
| helpers.setFieldError('email', messages.somethingWrong) | ||
| } | ||
| } | ||
| }, | ||
| [setPage, onComplete, audiusBackend] | ||
| ) | ||
| const onSubmit = useCallback( | ||
| async ( | ||
| values: ChangeEmailFormValues, | ||
| helpers: FormikHelpers<ChangeEmailFormValues> | ||
| ) => { | ||
| if (page === ChangeEmailPage.ConfirmPassword) { | ||
| await checkPassword(values, helpers) | ||
| } else if (page === ChangeEmailPage.VerifyEmail) { | ||
| await changeEmail(values, helpers) | ||
| } else { | ||
| await changeEmail(values, helpers) | ||
| } | ||
| }, | ||
| [page, changeEmail, checkPassword] | ||
| ) | ||
| return { | ||
| initialValues, | ||
| validateOnChange: false, | ||
| validationSchema, | ||
| onSubmit, | ||
| page, | ||
| setPage | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool idea!