-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLoginForm.tsx
More file actions
120 lines (104 loc) · 3.26 KB
/
Copy pathLoginForm.tsx
File metadata and controls
120 lines (104 loc) · 3.26 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
import Router from "next/router";
import React, { useEffect } from "react";
import { mutate } from "swr";
import ListErrors from "../common/ListErrors";
import UserAPI from "../../lib/api/user";
import { CODE_URL, STATE, SCOPE, GITHUB_CLIENT } from "../../lib/utils/constant";
const LoginForm = () => {
const [isLoading, setLoading] = React.useState(false);
const [errors, setErrors] = React.useState([]);
const [email, setEmail] = React.useState("");
const [password, setPassword] = React.useState("");
const handleEmailChange = React.useCallback(
(e) => setEmail(e.target.value),
[]
);
const handlePasswordChange = React.useCallback(
(e) => setPassword(e.target.value),
[]
);
const authorize_url = CODE_URL + "?client_id=" + GITHUB_CLIENT + "&scope=" + SCOPE
+ "&state=" + encodeURIComponent(STATE);
let logging_in;
if (typeof window !== "undefined"){
const code = new URLSearchParams(window.location.search).get("code");
const state = new URLSearchParams(window.location.search).get("state");
if (code){
logging_in = (<p>Redirecting to home page...</p>);
useEffect(() => {
async function post_code(){
try{
const {data, status} = await UserAPI.post_code(code, state);
console.log("begun await");
if (data?.user){
console.log(data.user)
window.localStorage.setItem("user", JSON.stringify(data.user));
mutate("user", data?.user);
Router.push("/");
}
} catch(error){
console.error(error);
}
}
post_code();
}, [])
}
}
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
try {
const { data, status } = await UserAPI.login(email, password);
if (status !== 200) {
setErrors(data.errors);
}
if (data?.user) {
window.localStorage.setItem("user", JSON.stringify(data.user));
mutate("user", data?.user);
Router.push("/");
}
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
return (
<>
<ListErrors errors={errors} />
<form onSubmit={handleSubmit}>
<fieldset>
<fieldset className="form-group">
<input
className="form-control form-control-lg"
type="email"
placeholder="Email"
value={email}
onChange={handleEmailChange}
/>
</fieldset>
<fieldset className="form-group">
<input
className="form-control form-control-lg"
type="password"
placeholder="Password"
value={password}
onChange={handlePasswordChange}
/>
</fieldset>
<button
className="btn btn-lg btn-primary pull-xs-right"
type="submit"
disabled={isLoading}
>
Sign in
</button>
</fieldset>
</form>
<a href={authorize_url} className="btn btn-lg btn-primary pull-xs-right">
Sign in through Github</a>
{logging_in}
</>
);
}
export default LoginForm;