Handle an error response for incorrect credentials.
This commit is contained in:
@@ -4,38 +4,37 @@ import { Paper, Text, TextInput, PasswordInput, Button, rem} from "@mantine/core
|
|||||||
import {useState} from 'react';
|
import {useState} from 'react';
|
||||||
import {http} from "../middleware/axiosConfig";
|
import {http} from "../middleware/axiosConfig";
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
import {useForm} from 'react-hook-form'
|
||||||
|
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const [userField, setUserField] = useState({
|
const {register, handleSubmit, } = useForm({
|
||||||
email:"",
|
defaultValue: {
|
||||||
password:""
|
email: '',
|
||||||
});
|
password: ''
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const changeFieldHandler = (e) => {
|
const {formState: { errors }} = useForm();
|
||||||
setUserField({
|
|
||||||
...userField,
|
const [loginError, setloginError] = useState(false);
|
||||||
[e.target.email]: e.target.value
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const onLogin = async (e) => {
|
const onLogin = async (e) => {
|
||||||
try {
|
try {
|
||||||
|
setloginError(false);
|
||||||
await http.get("/sanctum/csrf-cookie");
|
await http.get("/sanctum/csrf-cookie");
|
||||||
const res = await http.post("/api/login", {
|
const res = await http.post("/api/login", e);
|
||||||
email: e.email,
|
|
||||||
password: e.password,
|
|
||||||
});
|
|
||||||
if (res.status === 200){
|
if (res.status === 200){
|
||||||
navigate("/lists");
|
navigate("/lists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (error) {
|
||||||
console.error(err)
|
if (error.response.status === 401) {
|
||||||
console.log("Something's Wrong!");
|
setloginError(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,7 +43,7 @@ const Login = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Paper radius="md" p="xl" withBorder>
|
<Paper shadow="md" radius="md" p="xl" withBorder>
|
||||||
<Text size="xl" fw={900} variant="gradient"
|
<Text size="xl" fw={900} variant="gradient"
|
||||||
gradient={{ from: 'red', to: 'rgba(227, 0, 0, 1)', deg: 227 }}>WELCOME ADMIN!</Text>
|
gradient={{ from: 'red', to: 'rgba(227, 0, 0, 1)', deg: 227 }}>WELCOME ADMIN!</Text>
|
||||||
<Text size="sm">Please login to access the Admin Account Management Site</Text>
|
<Text size="sm">Please login to access the Admin Account Management Site</Text>
|
||||||
@@ -52,26 +51,34 @@ const Login = () => {
|
|||||||
<TextInput
|
<TextInput
|
||||||
style={{ width: '400px', marginBottom: '10px', marginTop: '15px' }}
|
style={{ width: '400px', marginBottom: '10px', marginTop: '15px' }}
|
||||||
label="Email"
|
label="Email"
|
||||||
leftSectionPointerEvents="none"
|
|
||||||
leftSection={icon}
|
leftSection={icon}
|
||||||
withAsterisk
|
{...register('email')}
|
||||||
onChange={e => changeFieldHandler(e)}
|
|
||||||
placeholder="Enter your work email address"
|
placeholder="Enter your work email address"
|
||||||
|
error={errors.email ? "Please enter a valid email" : null}
|
||||||
/></div>
|
/></div>
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
<PasswordInput
|
<PasswordInput
|
||||||
style={{ width: '400px', marginBottom: '10px' }}
|
style={{ width: '400px', marginBottom: '10px' }}
|
||||||
label="Password"
|
label="Password"
|
||||||
placeholder="Enter your Password"
|
placeholder="Enter your Password"
|
||||||
withAsterisk
|
{...register('password')}
|
||||||
onChange={e => changeFieldHandler(e)}
|
|
||||||
visible={visible}
|
visible={visible}
|
||||||
onVisibilityChange={toggle}
|
onVisibilityChange={toggle}
|
||||||
|
error={
|
||||||
|
errors.password
|
||||||
|
? "Please enter a valid password"
|
||||||
|
: null
|
||||||
|
}
|
||||||
/></div>
|
/></div>
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
<Button variant="filled" color="lime" style={{ margin: 25 }} onClick={e => onLogin(e)}>Log In
|
<Button variant="filled" color="lime" style={{ margin: 25 }} onClick={e => handleSubmit(onLogin)()}>Log In
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
|
{loginError && (
|
||||||
|
<Text fw={500} fs="italic" c="red">
|
||||||
|
Login failed! Please verify your credentials and try again.
|
||||||
|
</Text>)}</div>
|
||||||
</Paper>
|
</Paper>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user