LoginPage.js
3.71 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
132
133
134
import React, { useCallback, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import { login } from "../../../modules/user";
import { address } from "../../../modules/weather";
import "../style/LoginPage.scss"
function LoginPage(props) {
const dispatch = useDispatch();
const navigate = useNavigate();
const loginResult = useSelector((state) => state.user.loginData);
const [Id, setId] = useState("");
const [Password, setPassword] = useState("");
const [checkLogin, setCheckLogin] = useState(false);
const [checkIdError, setCheckIdError] = useState(false);
const [checkPasswordError, setCheckPasswordError] = useState(false);
const [checkLoginError, setCheckLoginError] = useState(false);
const idRegex = /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,14}$/;
const passwordRegex = /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-?])(?=.*[0-9]).{8,25}$/;
useEffect(() => {
if (checkLogin === true) {
loginResult.then((result) => {
if (result.loginSuccess === true) {
alert('로그인에 성공하였습니다.');
navigate('/main');
}
else {
alert('로그인에 실패하였습니다.');
}
})
}
}, [loginResult])
const onIdHandler = useCallback((event) => {
setId(event.currentTarget.value);
// 아이디 유효성 검사
if (!idRegex.test(event.currentTarget.value)) {
setCheckIdError(true);
}
else {
setCheckIdError(false);
}
}, [checkIdError]);
const onPasswordHandler = useCallback((event) => {
setPassword(event.currentTarget.value);
// 비밀번호 유효성 검사
if (!passwordRegex.test(event.currentTarget.value)) {
setCheckPasswordError(true);
}
else {
setCheckPasswordError(false);
}
}, [checkPasswordError]);
const onSubmitHandler = useCallback((event) => {
event.preventDefault();
if (checkIdError || Id === "") {
setCheckLoginError(true);
}
else if (checkPasswordError || Password === "") {
setCheckLoginError(true);
}
else {
setCheckLoginError(false);
}
// login
if (!checkLoginError) {
const UserData = {
id: Id,
password: Password
};
dispatch(login(UserData));
dispatch(address());
setCheckLogin(true);
}
}, [checkIdError, checkPasswordError, Password, dispatch, loginResult]);
return (
<div id = "body">
<div className="login-box">
<h2>로그인</h2>
<div className="input-area">
<input
placeholder="아이디"
type="text"
value={Id}
onChange={onIdHandler}
/>
</div>
<div className="check-variable">
{checkIdError && <div style={{color : 'red'}}>아이디는 6자리 이상 14자리 이하 소문자와 숫자로 입력해주세요.</div>}
</div>
<div className="input-area">
<input
placeholder="비밀번호"
type="text"
value={Password}
onChange={onPasswordHandler}
/>
</div>
<div className="check-variable">
{checkPasswordError && <div style={{color : 'red'}}>알파벳과 숫자, 특수문자를 포함하여 8자리 이상 입력해주세요.</div>}
</div>
<div className="btn-area" onClick={onSubmitHandler}>
<button
className="login-btn"
>
로그인
</button>
</div>
<div className="check-variable">
{checkLoginError && <div style={{color : 'red'}}>정보를 제대로 입력해주세요.</div>}
</div>
</div>
</div>
);
}
export default LoginPage;