Files
parcer/src/pages/Login.tsx

135 lines
3.6 KiB
TypeScript
Raw Normal View History

import axios from "axios";
2024-11-16 21:56:03 +05:30
import { useState, useContext, useEffect, FormEvent } from "react";
import { useNavigate, Link } from "react-router-dom";
import { AuthContext } from "../context/auth";
2024-11-16 23:21:59 +05:30
import { Box, Typography, TextField, Button, CircularProgress, Grid } from "@mui/material";
2024-09-25 13:19:47 +05:30
import { useGlobalInfoStore } from "../context/globalInfo";
2024-11-01 08:26:13 +05:30
import { apiUrl } from "../apiConfig";
2024-09-25 13:19:47 +05:30
2024-09-25 13:20:25 +05:30
const Login = () => {
const [form, setForm] = useState({
email: "",
password: "",
});
const [loading, setLoading] = useState(false);
const { notify } = useGlobalInfoStore();
const { email, password } = form;
2024-09-25 13:19:47 +05:30
const { state, dispatch } = useContext(AuthContext);
const { user } = state;
2024-09-25 13:19:47 +05:30
const navigate = useNavigate();
2024-09-25 13:19:47 +05:30
useEffect(() => {
if (user) {
navigate("/");
}
}, [user, navigate]);
2024-09-25 13:19:47 +05:30
2024-11-16 23:21:59 +05:30
const handleChange = (e: any) => {
const { name, value } = e.target;
setForm({ ...form, [name]: value });
};
2024-09-25 13:19:47 +05:30
2024-11-16 23:21:59 +05:30
const submitForm = async (e: any) => {
e.preventDefault();
setLoading(true);
try {
const { data } = await axios.post(`${apiUrl}/auth/login`, {
email,
password,
});
dispatch({ type: "LOGIN", payload: data });
notify("success", "Welcome to Maxun!");
window.localStorage.setItem("user", JSON.stringify(data));
navigate("/");
2024-11-16 21:56:03 +05:30
} catch (err) {
notify("error", "Login Failed. Please try again.");
setLoading(false);
}
};
2024-09-25 13:19:47 +05:30
return (
2024-11-16 23:21:59 +05:30
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
maxHeight: "100vh",
mt: 6,
padding: 4,
}}
>
2024-11-16 21:56:03 +05:30
<Box
component="form"
onSubmit={submitForm}
sx={{
2024-11-16 21:56:03 +05:30
textAlign: "center",
2024-11-16 23:21:59 +05:30
backgroundColor: "#ffffff",
2024-11-19 03:20:54 +05:30
padding: 6,
borderRadius: 5,
boxShadow: "0px 20px 40px rgba(0, 0, 0, 0.2), 0px -5px 10px rgba(0, 0, 0, 0.15)",
2024-11-16 21:56:03 +05:30
display: "flex",
flexDirection: "column",
2024-11-16 23:21:59 +05:30
alignItems: "center",
maxWidth: 400,
width: "100%",
2024-11-16 21:56:03 +05:30
}}
>
2024-11-16 23:21:59 +05:30
<img src="../src/assets/maxunlogo.png" alt="logo" height={55} width={60} style={{ marginBottom: 20, borderRadius: "20%", alignItems: "center" }} />
<Typography variant="h4" gutterBottom>
2024-11-16 21:56:03 +05:30
Welcome Back!
</Typography>
<TextField
fullWidth
label="Email"
name="email"
value={email}
onChange={handleChange}
margin="normal"
variant="outlined"
required
/>
<TextField
fullWidth
label="Password"
name="password"
type="password"
value={password}
onChange={handleChange}
margin="normal"
variant="outlined"
required
/>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
sx={{ mt: 2, mb: 2 }}
disabled={loading || !email || !password}
>
{loading ? (
<>
<CircularProgress size={20} sx={{ mr: 2 }} />
Loading
</>
) : (
"Login"
)}
</Button>
<Typography variant="body2" align="center">
Dont have an account?{" "}
<Link to="/register" style={{ textDecoration: "none", color: "#ff33cc" }}>
Register
</Link>
</Typography>
</Box>
</Box>
2024-11-16 23:21:59 +05:30
);
2024-09-25 13:19:47 +05:30
};
2024-09-25 13:20:25 +05:30
export default Login;