From f0475cc4cf8c4054923897397ec534aa69935408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrique=20Guimar=C3=A3es=20Ribeiro?= Date: Wed, 31 Jan 2024 16:40:02 -0300 Subject: [PATCH] fix: Login email verification flow (#31583) Co-authored-by: Hugo Costa <20212776+hugocostadev@users.noreply.github.com> --- .changeset/flat-windows-juggle.md | 6 +++++ apps/meteor/tests/e2e/page-objects/auth.ts | 1 - .../src/EmailConfirmationForm.tsx | 6 +++-- .../web-ui-registration/src/LoginForm.tsx | 22 +++++++++---------- 4 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 .changeset/flat-windows-juggle.md diff --git a/.changeset/flat-windows-juggle.md b/.changeset/flat-windows-juggle.md new file mode 100644 index 000000000000..4a0410b7fab1 --- /dev/null +++ b/.changeset/flat-windows-juggle.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": patch +"@rocket.chat/web-ui-registration": patch +--- + +Fixed login email verification flow when a user tries to join with username diff --git a/apps/meteor/tests/e2e/page-objects/auth.ts b/apps/meteor/tests/e2e/page-objects/auth.ts index 98421f6461ab..51290d46f9cc 100644 --- a/apps/meteor/tests/e2e/page-objects/auth.ts +++ b/apps/meteor/tests/e2e/page-objects/auth.ts @@ -15,7 +15,6 @@ export class Registration { return this.page.locator('role=button[name="Reset"]'); } - get btnLogin(): Locator { return this.page.locator('role=button[name="Login"]'); } diff --git a/packages/web-ui-registration/src/EmailConfirmationForm.tsx b/packages/web-ui-registration/src/EmailConfirmationForm.tsx index 680d90897632..3355081caa31 100644 --- a/packages/web-ui-registration/src/EmailConfirmationForm.tsx +++ b/packages/web-ui-registration/src/EmailConfirmationForm.tsx @@ -9,6 +9,9 @@ import { useLoginSendEmailConfirmation } from './hooks/useLoginSendEmailConfirma export const EmailConfirmationForm = ({ email, onBackToLogin }: { email?: string; onBackToLogin: () => void }): ReactElement => { const { t } = useTranslation(); + const basicEmailRegex = /^[^@]+@[^@]+$/; + const isEmail = basicEmailRegex.test(email || ''); + const { register, handleSubmit, @@ -17,7 +20,7 @@ export const EmailConfirmationForm = ({ email, onBackToLogin }: { email?: string email: string; }>({ defaultValues: { - email, + email: isEmail ? email : '', }, }); @@ -45,7 +48,6 @@ export const EmailConfirmationForm = ({ email, onBackToLogin }: { email?: string {...register('email', { required: true, })} - disabled={Boolean(email)} error={errors.email && t('registration.component.form.requiredField')} aria-invalid={errors?.email?.type === 'required'} placeholder={t('registration.component.form.emailPlaceholder')} diff --git a/packages/web-ui-registration/src/LoginForm.tsx b/packages/web-ui-registration/src/LoginForm.tsx index 94ea007344f6..55c638df6707 100644 --- a/packages/web-ui-registration/src/LoginForm.tsx +++ b/packages/web-ui-registration/src/LoginForm.tsx @@ -66,7 +66,7 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute clearErrors, getValues, formState: { errors }, - } = useForm<{ username: string; password: string }>({ + } = useForm<{ usernameOrEmail: string; password: string }>({ mode: 'onBlur', }); @@ -83,12 +83,12 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute useDocumentTitle(t('registration.component.login'), false); const loginMutation = useMutation({ - mutationFn: (formData: { username: string; password: string }) => { - return login(formData.username, formData.password); + mutationFn: (formData: { usernameOrEmail: string; password: string }) => { + return login(formData.usernameOrEmail, formData.password); }, onError: (error: any) => { if ([error.error, error.errorType].includes('error-invalid-email')) { - setError('username', { type: 'invalid-email', message: t('registration.page.login.errors.invalidEmail') }); + setError('usernameOrEmail', { type: 'invalid-email', message: t('registration.page.login.errors.invalidEmail') }); } if ('error' in error && error.error !== 403) { @@ -119,8 +119,8 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute ); }; - if (errors.username?.type === 'invalid-email') { - return clearErrors('username')} email={getValues('username')} />; + if (errors.usernameOrEmail?.type === 'invalid-email') { + return clearErrors('usernameOrEmail')} email={getValues('usernameOrEmail')} />; } return ( @@ -144,19 +144,19 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute - {errors.username && ( + {errors.usernameOrEmail && ( - {errors.username.message} + {errors.usernameOrEmail.message} )}