SignupPage.js
3.84 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
import React, {useState} from 'react';
import { Button, Form, FormGroup, Label, Input} from 'reactstrap';
const SigninPage = (props) => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [passwordCheck, setPasswordCheck] = useState('');
const onSubmit = (e) => {
e.preventDefault();
// 비밀번호가 6자이상인지 검사
if(password.length < 6 || passwordCheck.length < 6) {
return alert('비밀번호는 6자이상이어야 합니다.')
}
// 비밀번호와 비밀번호 체크가 다를 경우를 검사
if(password !== passwordCheck){
return alert('비밀번호가 일치하지 않습니다.');
}
const signupInfo = {
"username": username,
"password": password
};
const signup_info = {
method: "POST",
body: JSON.stringify(signupInfo),
headers: {
"Content-Type": "application/json",
'Accept': 'application/json'
}
};
if( username && password ) {
fetch("/api/signup", signup_info)
.then(response => response.json())
.then(json => {
if(json.message === 'success') {
alert('회원가입에 성공했습니다.');
props.history.push('/signin');
}
else if(json.message === 'user exist') {
alert('이미 존재하는 유저입니다');
setUsername('');
setPassword('');
} else {
alert('회원가입에 실패했습니다.');
setUsername('');
setPassword('');
}
})
}
};
const onChangeUsername = (e) => {
setUsername(e.target.value);
};
const onChangePassword = (e) => {
setPassword(e.target.value);
};
const onChangePasswordCheck = (e) => {
setPasswordCheck(e.target.value);
};
return (
<>
<Form onSubmit={onSubmit}
style={{
width:'100%',
maxWidth:'330px',
padding:'15px',
margin:'auto',
height:'100%'
}}>
<a href='/' style={{'color':'#000000', textDecoration:'none'}}>
<h1 className="text-center">
<span className="font-weight-bold">MEALKHU</span>.com
</h1>
</a>
<h2 className="text-center"><br/>Sign Up</h2>
<FormGroup>
<Label>Username</Label>
<Input required="required" value={username} onChange={onChangeUsername} name="username" type="name" placeholder="Enter your name"></Input>
</FormGroup>
<FormGroup>
<Label>Password</Label>
<Input required="required" type="password" value={password} onChange={onChangePassword} name="password" placeholder="Enter your password (length >= 6)"></Input>
</FormGroup>
<FormGroup>
<Label>Confirm Password</Label>
<Input required="required" type="password" value={passwordCheck} onChange={onChangePasswordCheck} name="confirm_password" placeholder="Enter your password again"></Input>
</FormGroup>
<FormGroup>
<Button type="submit" className="btn-lg btn-dark btn-block">Sign up</Button>
</FormGroup>
<div className="text-center mt-3">
<a href="/signin">Sign in here!</a>
</div>
</Form>
</>
);
}
export default SigninPage;