From 1b2dd00ed0349e3644ac3d50834a405379acdc9a Mon Sep 17 00:00:00 2001 From: Elias Biagioni Date: Fri, 30 Aug 2024 16:44:21 -0300 Subject: [PATCH] Update reset password fe in React --- .../src/pages/request-password-reset.tsx | 81 +++++++------------ .../web/react/src/pages/reset-password.tsx | 23 ++++-- .../web/react/src/services/user/api.ts | 6 +- .../web/react/src/services/user/forms.ts | 16 ++-- .../clients/web/react/src/utils/routes.tsx | 2 +- 5 files changed, 54 insertions(+), 74 deletions(-) diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/pages/request-password-reset.tsx b/{{cookiecutter.project_slug}}/clients/web/react/src/pages/request-password-reset.tsx index 31ef49e1c..281219095 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/pages/request-password-reset.tsx +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/pages/request-password-reset.tsx @@ -16,7 +16,6 @@ import { getErrorMessages } from 'src/utils/errors' export const RequestPasswordResetInner = () => { const [errorMessage, setErrorMessage] = useState() - const [resetLinkSent, setResetLinkSent] = useState(false) const { createFormFieldChangeHandler, form } = useTnForm() const navigate = useNavigate() @@ -24,7 +23,7 @@ export const RequestPasswordResetInner = () => { mutationFn: userApi.csc.requestPasswordReset, onSuccess: (data) => { setErrorMessage(undefined) - setResetLinkSent(true) + navigate('/password/reset/confirm/' + form.email.value) }, onError(e: any) { const errors = getErrorMessages(e) @@ -42,58 +41,32 @@ export const RequestPasswordResetInner = () => { return (
- {resetLinkSent ? ( - <> -

- Your request has been submitted. If there is an account associated with the email - provided, you should receive an email momentarily with instructions to reset your - password. -

-

- If you do not see the email in your main folder soon, please make sure to check your - spam folder. -

-
- -
- - ) : ( - <> -
{ - e.preventDefault() - }} - className="flex flex-col gap-2" - > - createFormFieldChangeHandler(form.email)(e.target.value)} - value={form.email.value ?? ''} - data-cy="email" - id="id" - label="Email address" - /> - -
- {errorMessage} -
- - - - )} +
{ + e.preventDefault() + }} + className="flex flex-col gap-2" + > + createFormFieldChangeHandler(form.email)(e.target.value)} + value={form.email.value ?? ''} + data-cy="email" + id="id" + label="Email address" + /> + +
+ {errorMessage} +
+ +
) diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/pages/reset-password.tsx b/{{cookiecutter.project_slug}}/clients/web/react/src/pages/reset-password.tsx index 1f27a30d3..d59ca36ad 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/pages/reset-password.tsx +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/pages/reset-password.tsx @@ -12,8 +12,7 @@ import { ResetPasswordForm, TResetPasswordForm, userApi } from 'src/services/use export const ResetPasswordInner = () => { const { form, createFormFieldChangeHandler, overrideForm } = useTnForm() - const { userId, token } = useParams() - console.log(userId, token) + const { userEmail } = useParams() const [error, setError] = useState('') const [success, setSuccess] = useState(false) @@ -32,10 +31,10 @@ export const ResetPasswordInner = () => { }) useEffect(() => { - if (token && userId) { - overrideForm(ResetPasswordForm.create({ token: token, uid: userId }) as TResetPasswordForm) + if (userEmail) { + overrideForm(ResetPasswordForm.create({ email: userEmail }) as TResetPasswordForm) } - }, [overrideForm, token, userId]) + }, [overrideForm, userEmail]) const onSubmit = (e: FormEvent) => { e.preventDefault() @@ -43,8 +42,8 @@ export const ResetPasswordInner = () => { if (form.isValid) { confirmResetPassword({ - userId: form.value.uid!, - token: form.value.token!, + email: form.value.email!, + code: form.value.code!, password: form.value.password!, }) } @@ -63,10 +62,18 @@ export const ResetPasswordInner = () => { ) } return ( - +
+ createFormFieldChangeHandler(form.code)(e.target.value)} + value={form.code.value ?? ''} + data-cy="code" + id="code" + /> + createFormFieldChangeHandler(form.password)(e.target.value)} diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/api.ts b/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/api.ts index 7a5d5bacc..c3224e4f3 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/api.ts +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/api.ts @@ -39,11 +39,11 @@ const requestPasswordReset = createCustomServiceCall({ }) const resetPassword = createCustomServiceCall({ - inputShape: { userId: z.string(), token: z.string(), password: z.string() }, + inputShape: { email: z.string(), code: z.string(), password: z.string() }, outputShape: userShape, cb: async ({ client, input, utils }) => { - const { token, user_id, ...rest } = utils.toApi(input) - const res = await client.post(`/password/reset/confirm/${user_id}/${token}/`, rest) + const { email, ...rest } = utils.toApi(input) + const res = await client.post(`/password/reset/confirm/${email}/`, rest) return utils.fromApi(res.data) }, }) diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/forms.ts b/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/forms.ts index eeddae331..3bca38c36 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/forms.ts +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/services/user/forms.ts @@ -89,24 +89,24 @@ export class EmailForgotPasswordForm extends Form { export type TEmailForgotPasswordForm = EmailForgotPasswordForm & EmailForgotPasswordInput export type ResetPasswordInput = { - uid: IFormField - token: IFormField + email: IFormField + code: IFormField password: IFormField confirmPassword: IFormField } export class ResetPasswordForm extends Form { - static uid = new FormField({ - label: 'UID', - placeholder: 'uid', + static email = new FormField({ + label: 'Email', + placeholder: 'email', type: 'text', validators: [new RequiredValidator({ message: 'Please enter a valid uid' })], }) - static token = new FormField({ - placeholder: 'Verification Token', + static code = new FormField({ + placeholder: 'Verification Code', type: 'text', validators: [ - new MinLengthValidator({ message: 'Please enter a valid 5 digit code', minLength: 5 }), + new MinLengthValidator({ message: 'Please enter a valid 7 digit code', minLength: 7 }), ], }) static password = FormField.create({ diff --git a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/routes.tsx b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/routes.tsx index 855c23dec..ed903b38e 100644 --- a/{{cookiecutter.project_slug}}/clients/web/react/src/utils/routes.tsx +++ b/{{cookiecutter.project_slug}}/clients/web/react/src/utils/routes.tsx @@ -21,7 +21,7 @@ const AuthRoutes = () => { } /> } /> } /> - } /> + } /> ) }