From 8b10c6cf0f48153a1c685bd1c00ac42b1c219166 Mon Sep 17 00:00:00 2001 From: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Date: Thu, 29 Feb 2024 16:08:22 -0300 Subject: [PATCH] fix: Login page breaking when handling not expected errors (#31804) --- .changeset/quick-cheetahs-help.md | 5 +++ .../web-ui-registration/src/LoginForm.tsx | 36 ++++++++++++------- .../web-ui-registration/src/LoginServices.tsx | 4 +-- .../src/LoginServicesButton.tsx | 8 ++--- 4 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 .changeset/quick-cheetahs-help.md diff --git a/.changeset/quick-cheetahs-help.md b/.changeset/quick-cheetahs-help.md new file mode 100644 index 000000000000..12005d73ec1a --- /dev/null +++ b/.changeset/quick-cheetahs-help.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/web-ui-registration": patch +--- + +fixed the login page crashing when receiving unexpected errors diff --git a/packages/web-ui-registration/src/LoginForm.tsx b/packages/web-ui-registration/src/LoginForm.tsx index 3290b9f16191..d63886091fa0 100644 --- a/packages/web-ui-registration/src/LoginForm.tsx +++ b/packages/web-ui-registration/src/LoginForm.tsx @@ -56,7 +56,9 @@ const LOGIN_SUBMIT_ERRORS = { }, } as const; -export type LoginErrors = keyof typeof LOGIN_SUBMIT_ERRORS | 'totp-canceled'; +export type LoginErrors = keyof typeof LOGIN_SUBMIT_ERRORS | 'totp-canceled' | string; + +export type LoginErrorState = [error: LoginErrors, message?: string] | undefined; export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRouter }): ReactElement => { const { @@ -72,7 +74,7 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute const { t } = useTranslation(); const formLabelId = useUniqueId(); - const [errorOnSubmit, setErrorOnSubmit] = useState(undefined); + const [errorOnSubmit, setErrorOnSubmit] = useState(undefined); const isResetPasswordAllowed = useSetting('Accounts_PasswordReset'); const login = useLoginWithPassword(); const showFormLogin = useSetting('Accounts_ShowFormLogin'); @@ -92,11 +94,11 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute } if ('error' in error && error.error !== 403) { - setErrorOnSubmit(error.error); + setErrorOnSubmit([error.error, error.reason]); return; } - setErrorOnSubmit('user-not-found'); + setErrorOnSubmit(['user-not-found']); }, }); @@ -110,18 +112,28 @@ export const LoginForm = ({ setLoginRoute }: { setLoginRoute: DispatchLoginRoute } }, [errorOnSubmit]); - const renderErrorOnSubmit = (error: LoginErrors) => { + const renderErrorOnSubmit = ([error, message]: Exclude) => { + if (error in LOGIN_SUBMIT_ERRORS) { + const { type, i18n } = LOGIN_SUBMIT_ERRORS[error as Exclude]; + return ( + + {t(i18n)} + + ); + } + if (error === 'totp-canceled') { return null; } - const { type, i18n } = LOGIN_SUBMIT_ERRORS[error]; - - return ( - - {t(i18n)} - - ); + if (message) { + return ( + + {message} + + ); + } + return null; }; if (errors.usernameOrEmail?.type === 'invalid-email') { diff --git a/packages/web-ui-registration/src/LoginServices.tsx b/packages/web-ui-registration/src/LoginServices.tsx index 530dce7e9438..a5068fbaac3f 100644 --- a/packages/web-ui-registration/src/LoginServices.tsx +++ b/packages/web-ui-registration/src/LoginServices.tsx @@ -3,7 +3,7 @@ import { useLoginServices, useSetting } from '@rocket.chat/ui-contexts'; import type { Dispatch, ReactElement, SetStateAction } from 'react'; import { useTranslation } from 'react-i18next'; -import type { LoginErrors } from './LoginForm'; +import type { LoginErrorState } from './LoginForm'; import LoginServicesButton from './LoginServicesButton'; const LoginServices = ({ @@ -11,7 +11,7 @@ const LoginServices = ({ setError, }: { disabled?: boolean; - setError: Dispatch>; + setError: Dispatch>; }): ReactElement | null => { const { t } = useTranslation(); const services = useLoginServices(); diff --git a/packages/web-ui-registration/src/LoginServicesButton.tsx b/packages/web-ui-registration/src/LoginServicesButton.tsx index cdba5e26474e..d9f43b0e484c 100644 --- a/packages/web-ui-registration/src/LoginServicesButton.tsx +++ b/packages/web-ui-registration/src/LoginServicesButton.tsx @@ -5,7 +5,7 @@ import { useLoginWithService, useTranslation } from '@rocket.chat/ui-contexts'; import type { ReactElement, SetStateAction, Dispatch } from 'react'; import { useCallback } from 'react'; -import type { LoginErrors } from './LoginForm'; +import type { LoginErrorState, LoginErrors } from './LoginForm'; const LoginServicesButton = ({ buttonLabelText, @@ -19,17 +19,17 @@ const LoginServicesButton = ({ }: T & { className?: string; disabled?: boolean; - setError?: Dispatch>; + setError?: Dispatch>; }): ReactElement => { const t = useTranslation(); const handler = useLoginWithService({ service, buttonLabelText, ...props }); const handleOnClick = useCallback(() => { - handler().catch((e: { error?: LoginErrors }) => { + handler().catch((e: { error?: LoginErrors; reason?: string }) => { if (!e.error || typeof e.error !== 'string') { return; } - setError?.(e.error); + setError?.([e.error, e.reason]); }); }, [handler, setError]);