2024-09-25 13:19:47 +05:30
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import { useState, useContext, useEffect } from 'react';
|
|
|
|
|
|
import { useNavigate, Link } from 'react-router-dom';
|
|
|
|
|
|
import { AuthContext } from '../context/auth';
|
2024-09-25 13:19:57 +05:30
|
|
|
|
import {
|
|
|
|
|
|
Box,
|
|
|
|
|
|
Typography,
|
|
|
|
|
|
TextField,
|
|
|
|
|
|
Button,
|
|
|
|
|
|
CircularProgress,
|
2024-09-25 13:19:47 +05:30
|
|
|
|
} from '@mui/material';
|
|
|
|
|
|
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 = () => {
|
2024-09-25 13:19:57 +05:30
|
|
|
|
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
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
const { state, dispatch } = useContext(AuthContext);
|
|
|
|
|
|
const { user } = state;
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
const navigate = useNavigate();
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (user) {
|
|
|
|
|
|
navigate('/');
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [user, navigate]);
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
const handleChange = (e: any) => {
|
|
|
|
|
|
const { name, value } = e.target;
|
|
|
|
|
|
setForm({ ...form, [name]: value });
|
|
|
|
|
|
};
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
const submitForm = async (e: any) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
try {
|
2024-11-01 08:26:13 +05:30
|
|
|
|
const { data } = await axios.post(`${apiUrl}/auth/login`, { email, password });
|
2024-09-25 13:19:57 +05:30
|
|
|
|
dispatch({ type: 'LOGIN', payload: data });
|
|
|
|
|
|
notify('success', 'Welcome to Maxun!');
|
|
|
|
|
|
window.localStorage.setItem('user', JSON.stringify(data));
|
|
|
|
|
|
navigate('/');
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
|
notify('error', err.response.data || 'Login Failed. Please try again.');
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
return (
|
|
|
|
|
|
<Box
|
|
|
|
|
|
sx={{
|
|
|
|
|
|
display: 'flex',
|
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
|
alignItems: 'center',
|
2024-11-16 15:51:25 +05:30
|
|
|
|
justifyContent: 'center',
|
2024-11-16 16:13:54 +05:30
|
|
|
|
backgroundColor: '#f5f5f5',
|
2024-11-16 15:51:25 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
height: "calc(100vh - 64px)",
|
|
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
}}
|
|
|
|
|
|
>
|
2024-11-16 16:13:54 +05:30
|
|
|
|
<Box component="form" onSubmit={submitForm} sx={{ textAlign: 'center',maxWidth: 400, width: '100%', backgroundColor: '#fff', padding: 3, borderRadius: 4,boxShadow: '0px 2px 4px rgba(0, 0, 0, 0.1)', display: 'flex', flexDirection: 'column' }}>
|
|
|
|
|
|
<Typography variant="h4" gutterBottom color={"#ff00c3"}>
|
2024-09-25 17:04:56 +05:30
|
|
|
|
Welcome Back!
|
2024-09-25 13:19:57 +05:30
|
|
|
|
</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
|
|
|
|
|
|
/>
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
<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>
|
2024-09-25 13:19:47 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
<Typography variant="body2" align="center">
|
|
|
|
|
|
Don’t have an account?{' '}
|
2024-10-24 07:21:12 +05:30
|
|
|
|
<Link to="/register" style={{ textDecoration: 'none'}}>
|
2024-09-25 13:19:57 +05:30
|
|
|
|
Register
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
</Typography>
|
|
|
|
|
|
</Box>
|
2024-11-16 15:51:25 +05:30
|
|
|
|
|
2024-09-25 13:19:57 +05:30
|
|
|
|
</Box>
|
|
|
|
|
|
);
|
2024-09-25 13:19:47 +05:30
|
|
|
|
};
|
|
|
|
|
|
|
2024-09-25 13:20:25 +05:30
|
|
|
|
export default Login;
|