-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathLogin.js
More file actions
131 lines (123 loc) · 3.49 KB
/
Login.js
File metadata and controls
131 lines (123 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Box, Button, TextField, Typography } from "@mui/material";
import React, { useEffect, useState } from "react";
import axios from "axios";
import { useDispatch } from "react-redux";
import { authActions } from "../store";
import { useNavigate, useLocation } from "react-router-dom";
import config from "../config";
const Login = () => {
const location = useLocation();
const navigate = useNavigate();
const dispatch = useDispatch();
const { isSignupButtonPressed } = location.state || {};
const [inputs, setInputs] = useState({
name: "",
email: "",
password: "",
});
const [isSignup, setIsSignup] = useState(isSignupButtonPressed || false);
useEffect(() => {
setIsSignup(isSignupButtonPressed);
}, [isSignupButtonPressed]);
const handleChange = (e) => {
setInputs((prevState) => ({
...prevState,
[e.target.name]: e.target.value,
}));
};
const sendRequest = async (type = "login") => {
try {
const res = await axios.post(`${config.BASE_URL}/api/users/${type}`, {
name: inputs.name,
email: inputs.email,
password: inputs.password,
});
return res.data;
} catch (err) {
console.error("Error in request:", err.response?.data || err.message);
alert(err.response?.data?.message || "Invalid credentials or server error");
}
};
const handleSubmit = (e) => {
e.preventDefault();
if (isSignup) {
sendRequest("signup").then((data) => {
if (data?.user?._id) {
localStorage.setItem("userId", data.user._id);
dispatch(authActions.login());
navigate("/blogs");
}
});
} else {
sendRequest("login").then((data) => {
if (data?.user?._id) {
localStorage.setItem("userId", data.user._id);
dispatch(authActions.login());
navigate("/blogs");
}
});
}
};
return (
<div>
<form onSubmit={handleSubmit}>
<Box
maxWidth={400}
display="flex"
flexDirection={"column"}
alignItems="center"
justifyContent={"center"}
boxShadow="10px 10px 20px #ccc"
padding={3}
margin="auto"
marginTop={5}
borderRadius={5}
>
<Typography variant="h2" padding={3} textAlign="center">
{isSignup ? "Signup" : "Login"}
</Typography>
{isSignup && (
<TextField
name="name"
onChange={handleChange}
value={inputs.name}
placeholder="Name"
margin="normal"
/>
)}
<TextField
name="email"
onChange={handleChange}
value={inputs.email}
type={"email"}
placeholder="Email"
margin="normal"
/>
<TextField
name="password"
onChange={handleChange}
value={inputs.password}
type={"password"}
placeholder="Password"
margin="normal"
/>
<Button
type="submit"
variant="contained"
sx={{ borderRadius: 3, marginTop: 3 }}
color="warning"
>
Submit
</Button>
<Button
onClick={() => setIsSignup(!isSignup)}
sx={{ borderRadius: 3, marginTop: 3 }}
>
Change To {isSignup ? "Login" : "Signup"}
</Button>
</Box>
</form>
</div>
);
};
export default Login;