Login.js
3.31 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
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import localforage from "localforage";
import "../styles/Login.css";
import axios from "axios";
import cryptoJs from "crypto-js";
const Login = () => {
// console.log("visit Login");
const [state, setState] = useState({
id: "",
pw: "",
btn: "Login",
});
const handleChangeState = (e) => {
setState({
...state,
[e.target.name]: e.target.value,
});
};
const navigate = useNavigate();
const login = async () => {
setState({ ...state, btn: "Login..." });
const { data: userDBID } = await axios.get(
`http://${process.env.REACT_APP_SERVER_IP}:3001/db/users`,
{ params: { loginID: state.id } }
);
if (userDBID) {
//pass crawling, just ical
const { data: peed } = await axios.get(
`http://${process.env.REACT_APP_SERVER_IP}:3001/db/users/ical`,
{ params: { ID: userDBID } }
);
await axios.put(
`http://${process.env.REACT_APP_SERVER_IP}:3001/db/ical`,
{
userID: userDBID,
peed: peed.ical,
}
);
const hashpw = cryptoJs.SHA256(state.pw).toString();
const { data: isCorrectPW } = await axios.get(
`http://${process.env.REACT_APP_SERVER_IP}:3001/db/users/check`,
{
params: { loginID: state.id, loginPW: hashpw },
}
);
if (isCorrectPW) await localforage.setItem("userID", Number(userDBID));
else {
setState({ ...state, btn: "Login" });
alert(
"등록된 ID/PW와 다릅니다\n(비밀번호가 변경되었다면 관리자에게 문의하세요)"
);
return;
}
} else {
//crawling
const { data: loginResult } = await axios.post(
`http://${process.env.REACT_APP_SERVER_IP}:3001/login/`,
{
id: state.id,
pw: state.pw,
}
);
if (loginResult === "failed") {
setState({ ...state, btn: "Login" });
alert(
"ID/PW를 확인해주세요\n(크롤링 오류일 수 있습니다. 다시 시도해주세요)"
);
return;
} else if (loginResult === "error") {
setState({ ...state, btn: "Login" });
alert("다시 시도해 주세요");
return;
}
await localforage.setItem("userID", loginResult.userID);
}
await localforage.setItem("id", state.id);
await localforage.setItem("pw", state.pw);
await localforage.setItem("session", true);
navigate("/");
};
useEffect(() => {
async function ifAlreadyLogined() {
if (await localforage.getItem("session")) navigate("/");
}
ifAlreadyLogined();
}, [navigate]);
return (
<div className="Login">
<div className="idpw">
<input
className="id"
name="id"
value={state.id}
onChange={handleChangeState}
onFocus={(e) => e.target.select()}
placeholder="E-Campus ID"
/>
<input
className="pw"
name="pw"
value={state.pw}
onChange={handleChangeState}
onFocus={(e) => e.target.select()}
placeholder="E-Campus Password"
type="password"
/>
</div>
<button onClick={login}>{state.btn}</button>
</div>
);
};
export default Login;