-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathLogin.js
More file actions
129 lines (122 loc) · 3.52 KB
/
Copy pathLogin.js
File metadata and controls
129 lines (122 loc) · 3.52 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
import { Box, Button, TextField, Typography } from "@mui/material";
import React, { useEffect, useState } from "react";
import axios from "axios";
import bcrypt from 'bcryptjs';
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 naviagte = useNavigate();
const dispath = useDispatch();
const { isSignupButtonPressed } = location.state || {};
const [inputs, setInputs] = useState({
name: "",
email: "",
password: "",
});
const [isSignup, setIsSignup] = useState(isSignupButtonPressed || false);
const handleChange = (e) => {
setInputs((prevState) => ({
...prevState,
[e.target.name]: e.target.value,
}));
};
useEffect(() => {
setIsSignup(isSignupButtonPressed);
}, [isSignupButtonPressed]);
const sendRequest = async (type = "login",dataToSend) => {
let res;
try {
res = await axios.post(`${config.BASE_URL}/api/users/${type}`, dataToSend)
} catch (err) {
console.error("Signup error:", err);
return;
}
const data = await res.data;
return data;
};
const handleSubmit = (e) => {
e.preventDefault();
const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(inputs.password, salt);
const dataToSend = {
...inputs,
password: hashedPassword,
};
if (isSignup) {
sendRequest("signup", dataToSend)
.then((data) => localStorage.setItem("userId", data.user._id))
.then(() => dispath(authActions.login()))
.then(() => naviagte("/blogs"));
} else {
sendRequest("login", dataToSend)
.then((data) => localStorage.setItem("userId", data.user._id))
.then(() => dispath(authActions.login()))
.then(() => naviagte("/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;