| 1 | import {useState} from "react";
|
|---|
| 2 | import {useAuthContext} from "../../configurations/AuthContext";
|
|---|
| 3 | import {BasicAuth, LoginUser} from "../../services/user-service";
|
|---|
| 4 | import {Link, redirect, useNavigate} from "react-router-dom";
|
|---|
| 5 | import {toast} from "react-toastify";
|
|---|
| 6 | import {Avatar, Button, Grid, TextField, Typography} from "@mui/material";
|
|---|
| 7 | import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
|
|---|
| 8 | import LoginPhoto from "../../assets/images/delivery.jpg";
|
|---|
| 9 | import PersonIcon from '@mui/icons-material/Person';
|
|---|
| 10 |
|
|---|
| 11 | const Login = () => {
|
|---|
| 12 | const {loggedUser, login} = useAuthContext();
|
|---|
| 13 | const navigate = useNavigate();
|
|---|
| 14 | const [user, setUser] = useState({
|
|---|
| 15 | username: "",
|
|---|
| 16 | password: ""
|
|---|
| 17 | });
|
|---|
| 18 |
|
|---|
| 19 | const handleChange = name => event => {
|
|---|
| 20 | setUser({...user, [name]: event.target.value});
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | const handleSubmit = async event => {
|
|---|
| 24 | event.preventDefault();
|
|---|
| 25 | const response = await LoginUser(user);
|
|---|
| 26 |
|
|---|
| 27 | if (!response) {
|
|---|
| 28 | return;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | let authData = {
|
|---|
| 32 | userCredential: BasicAuth(response.username, response.password),
|
|---|
| 33 | userId: response.id,
|
|---|
| 34 | userRole: response.role,
|
|---|
| 35 | userRoleId: response.roleId,
|
|---|
| 36 | activeOwnershipId: response.activeOwnershipId
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | let roleUser = {
|
|---|
| 40 | roleId: response.roleId,
|
|---|
| 41 | role: response.role,
|
|---|
| 42 | activeOwnershipId: response.activeOwnershipId
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | sessionStorage.setItem('authData', JSON.stringify(authData));
|
|---|
| 46 |
|
|---|
| 47 | login(response.id, roleUser);
|
|---|
| 48 | navigate("/");
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | return (loggedUser ? redirect("/") :
|
|---|
| 52 | <Grid container component={"main"}>
|
|---|
| 53 | <Grid item xs={false} sm={4} md={7} className={"background-image login-text"}
|
|---|
| 54 | style={{backgroundImage: `url(${LoginPhoto})`}}> All Restaurants Delivery </Grid>
|
|---|
| 55 | <Grid item xs={12} sm={8} md={5} elevation={6} className={"flex-column-align-justify-center"}>
|
|---|
| 56 | <div className={"d-flex flex-column align-items-center"}>
|
|---|
| 57 | <Avatar>
|
|---|
| 58 | <PersonIcon/>
|
|---|
| 59 | </Avatar>
|
|---|
| 60 | <Typography component="h1" variant="h5">
|
|---|
| 61 | Sign in
|
|---|
| 62 | </Typography>
|
|---|
| 63 | <form className={"full-width mt-1"} onSubmit={handleSubmit}>
|
|---|
| 64 | <TextField
|
|---|
| 65 | onChange={handleChange("username")}
|
|---|
| 66 | variant="outlined"
|
|---|
| 67 | margin="normal"
|
|---|
| 68 | required
|
|---|
| 69 | fullWidth
|
|---|
| 70 | id="username"
|
|---|
| 71 | label="Username"
|
|---|
| 72 | name="username"
|
|---|
| 73 | autoFocus
|
|---|
| 74 | />
|
|---|
| 75 | <TextField
|
|---|
| 76 | onChange={handleChange("password")}
|
|---|
| 77 | variant="outlined"
|
|---|
| 78 | margin="normal"
|
|---|
| 79 | required
|
|---|
| 80 | fullWidth
|
|---|
| 81 | name="password"
|
|---|
| 82 | label="Password"
|
|---|
| 83 | type="password"
|
|---|
| 84 | id="password"
|
|---|
| 85 | />
|
|---|
| 86 | <Button
|
|---|
| 87 | type="submit"
|
|---|
| 88 | fullWidth
|
|---|
| 89 | variant="contained"
|
|---|
| 90 | color="primary"
|
|---|
| 91 | >
|
|---|
| 92 | Sign In
|
|---|
| 93 | </Button>
|
|---|
| 94 | <div className={"text-right"}>
|
|---|
| 95 | <Link to={"/register"}>
|
|---|
| 96 | Don't have an account? Sign Up
|
|---|
| 97 | </Link>
|
|---|
| 98 | </div>
|
|---|
| 99 | </form>
|
|---|
| 100 | </div>
|
|---|
| 101 | </Grid>
|
|---|
| 102 | </Grid>
|
|---|
| 103 | );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | export default Login; |
|---|