From e79198153e58dd46f69226139eed56348dce7e7a Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Thu, 19 Sep 2024 15:33:52 -0300 Subject: [PATCH] feat: error handling when login in issue #46 --- client/src/api-service/apiConfig.ts | 5 ++++- client/src/views/Login/index.tsx | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/client/src/api-service/apiConfig.ts b/client/src/api-service/apiConfig.ts index 7fa6459..ab5e0ae 100644 --- a/client/src/api-service/apiConfig.ts +++ b/client/src/api-service/apiConfig.ts @@ -58,9 +58,12 @@ const ApiConfig = { token: data.token }; } - if (response.status === 404) { + if (response.status === 403) { return new Error('Wrong username or password!'); } + if (response.status === 404) { + return new Error('Username not found!'); + } } catch (error) { if (typeof error === 'string') { diff --git a/client/src/views/Login/index.tsx b/client/src/views/Login/index.tsx index a372f35..69fd3eb 100644 --- a/client/src/views/Login/index.tsx +++ b/client/src/views/Login/index.tsx @@ -16,6 +16,7 @@ function Login(): JSX.Element { const navigate = useNavigate(); const [validated, setValidated] = useState(true); const [formInvalid, setFormInvalid] = useState(false); + const [errorMessage, setErrorMessage] = useState(''); /** * Handles the form submit button click event. @@ -35,8 +36,17 @@ function Login(): JSX.Element { } setFormInvalid(false); - await signIn(form.email.value, form.password.value); - goTo("/home"); + try { + await signIn(form.email.value, form.password.value); + goTo("/home"); + } catch (e) { + setFormInvalid(true); + if (typeof e === 'string') { + setErrorMessage(e); + } else if (e instanceof Error) { + setErrorMessage(e.message); + } + } }; /** @@ -60,7 +70,7 @@ function Login(): JSX.Element { {formInvalid ? ( - Invalid email or password. + { errorMessage } ) : null}