SignUp.js
4.53 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React, { useState } from 'react';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import {makeStyles, Link, Avatar, Button, CssBaseline, TextField, Grid, Typography, Container} from '@material-ui/core';
import axios from 'axios'
import {useHistory} from 'react-router-dom';
const header = {withCredentials: true}
const UseStyles = makeStyles((theme) => ({
paper: {
marginTop: theme.spacing(8),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
},
avatar: {
margin: theme.spacing(1),
backgroundColor: theme.palette.secondary.main,
},
form: {
width: '100%', // Fix IE 11 issue.
marginTop: theme.spacing(3),
},
submit: {
margin: theme.spacing(3, 0, 2),
},
}));
function SignUp() {
const classes = UseStyles();
const [email, setEamil] = useState();
const [password, setPassword] = useState();
const [re_password, setRePassword] = useState();
const history = useHistory();
const sendParams = {
header,
Email: email,
Password: password
}
const SignUpHandler = () => {
if(email === '' || email === undefined){
alert("이메일을 입력해 주세요");
}
else if(password === '' || password === undefined){
alert("비밀번호를 입력해 주세요");
}
else if(re_password === '' || re_password === undefined){
alert("비밀번호를 한번더 입력해 주세요");
}
else if(password !== re_password){
alert("비밀번호가 일치하지 않습니다.");
}
else{
axios
.post('http://localhost:3002/users/signup', sendParams)
.then(res => {
if(res.data.message){
alert(res.data.message);
if(res.data.duplicate === '1'){ // 이메일 중복
history.push('/signup');
}
else{ // 회원가입 성공
history.push('/');
}
}
else{
alert("회원 가입 실패");
}
})
.catch(err => {
console.log(err);
})
}
}
// useEffect(() => {
// async function fetchData(){
// const request = await axios.get('http://localhost:3002/api')
// console.log(request);
// return request;
// }
// fetchData();
// })
return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<Avatar className={classes.avatar}>
<LockOutlinedIcon />
</Avatar>
<Typography component="h1" variant="h5">
Sign up
</Typography>
<form className={classes.form} noValidate>
<Grid container spacing={3}>
<Grid item xs={12}>
<TextField
variant="outlined"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
onChange={res => setEamil(res.target.value)}
/>
</Grid>
<Grid item xs={12}>
<TextField
variant="outlined"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
onChange={res => setPassword(res.target.value)}
/>
</Grid>
<Grid item xs={12}>
<TextField
variant="outlined"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
onChange={res => setRePassword(res.target.value)}
/>
</Grid>
</Grid>
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
onClick={SignUpHandler}
href='#'
>
Sign Up
</Button>
<Grid container>
<Grid item>
<Link href="/" variant="body2">
{"Go to SignIn"}
</Link>
</Grid>
</Grid>
</form>
</div>
</Container>
);
}
export default SignUp