feat: error handling when login in

issue #46
This commit is contained in:
Ricardo Campos
2024-09-19 15:33:52 -03:00
parent aa0a7875b0
commit e79198153e
2 changed files with 17 additions and 4 deletions
+4 -1
View File
@@ -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') {
+13 -3
View File
@@ -16,6 +16,7 @@ function Login(): JSX.Element {
const navigate = useNavigate();
const [validated, setValidated] = useState<boolean>(true);
const [formInvalid, setFormInvalid] = useState<boolean>(false);
const [errorMessage, setErrorMessage] = useState<string>('');
/**
* 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 ? (
<Alert variant={"danger"}>
Invalid email or password.
{ errorMessage }
</Alert>
) : null}