login.jsx
1.97 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
import { Link } from "react-router-dom";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
// lib
import authService from "../service/auth";
// styles
import "../styles/layout.scss";
function Login() {
const [loginSuccess, setLoginSuccess] = useState(0);
const navigate = useNavigate();
const [userinfo, setUserinfo] = useState({
email: "",
password: "",
});
useEffect(() => {
if (sessionStorage.key("user-token")) {
navigate("/", { replace: true });
}
}, [loginSuccess, setLoginSuccess, navigate]);
const onChange = (e) => {
setUserinfo((orgState) => ({
...orgState,
[e.target.id]: e.target.value,
}));
};
const handleSubmit = async (e) => {
e.preventDefault();
if (!userinfo.email || !userinfo.password) {
alert("Please fill all boxes");
return;
}
const answer = await authService.handleLogin(userinfo);
setLoginSuccess(answer);
};
return (
<div className="container">
<h1>Welcome back!</h1>
<h2>Stay connected to weather*stories*friends</h2>
<button>Login with Google</button>
<button>Login with GitHub</button>
<br />
<form className="authForm" onSubmit={(e) => handleSubmit(e)}>
<label htmlFor="email">
<input
placeholder="email"
onChange={(e) => onChange(e)}
value={userinfo.email}
type="email"
id="email"
/>
</label>
<label htmlFor="password">
<input
placeholder="password"
onChange={(e) => onChange(e)}
value={userinfo.password}
type="password"
id="password"
/>
</label>
<label htmlFor="submit">
<input type="submit" id="submit" />
</label>
</form>
<br />
<p>
Don't have an account? <Link to="/signup">Create account</Link>
</p>
</div>
);
}
export default Login;