Signup.js 8.99 KB
Newer Older
이재연's avatar
이재연 committed
1
import React, { useState } from 'react';
이재연's avatar
aa    
이재연 committed
2
import axios from 'axios'
이재연's avatar
이재연 committed
3
import { Form, Col, Container, Button, Row, Alert } from 'react-bootstrap'
이재연's avatar
aa    
이재연 committed
4
import catchErrors from '../utils/catchErrors'
박상호's avatar
박상호 committed
5
import { Redirect } from 'react-router-dom';
이재연's avatar
이재연 committed
6

이재연's avatar
이재연 committed
7
8
9
10
11
12
13
14
const INIT_USER = {
    name: '',
    number: '',
    id: '',
    password: '',
    password2: '',
    tel: ''
}
Kim, Subin's avatar
Kim, Subin committed
15
16

function Signup() {
17
    const [user, setUser] = useState(INIT_USER)
이재연's avatar
이재연 committed
18
    const [error, setError] = useState('')
박상호's avatar
박상호 committed
19
    const [success, setSuccess] = useState(false)
20
    const [validated, setValidated] = useState(false);
이재연's avatar
이재연 committed
21

이재연's avatar
이재연 committed
22
23
24
    function handleChange(event) {
        const { name, value } = event.target
        setUser({ ...user, [name]: value })
이재연's avatar
이재연 committed
25
    }
이재연's avatar
이재연 committed
26
27


이재연's avatar
이재연 committed
28
29
30
    async function handleSubmit(event) {
        event.preventDefault()
        const form = event.currentTarget;
이재연's avatar
이재연 committed
31
32
        console.log(form)
        if (form.checkValidity() === false) {
이재연's avatar
이재연 committed
33
34
            event.preventDefault();
            event.stopPropagation();
이재연's avatar
이재연 committed
35
36
        }
        setValidated(true);
이재연's avatar
이재연 committed
37
        console.log(user)
이재연's avatar
aa    
이재연 committed
38

이재연's avatar
이재연 committed
39
40
        try {
            setError('')
이재연's avatar
aa    
이재연 committed
41
42
            const response = await axios.post('/api/users/signup', user)
            console.log(response.data)
박상호's avatar
박상호 committed
43
            setSuccess(true)
이재연's avatar
이재연 committed
44
        } catch (error) {
이재연's avatar
aa    
이재연 committed
45
            catchErrors(error, setError)
이재연's avatar
이재연 committed
46
        }
47

이재연's avatar
이재연 committed
48
    }
49
50
51
    function checkPassword(event) {
        const p1 = user.password
        const p2 = user.password2
이재연's avatar
이재연 committed
52

53
        if (p1 !== p2) {
이재연's avatar
이재연 committed
54
55
56
57
            event.preventDefault();
            event.stopPropagation();
            alert('비밀번호가 일치하지 않습니다.')
            return false
58
59

        } else {
이재연's avatar
이재연 committed
60
61
62
            return true
        }
    }
박상호's avatar
박상호 committed
63
64
65
66
    if (success) {
        alert('회원가입 되었습니다.')
        return <Redirect to='/login'/>
    }
67

이재연's avatar
..    
이재연 committed
68

Kim, Subin's avatar
Kim, Subin committed
69
    return (
이재연's avatar
logsign    
이재연 committed
70
71
        <div>
            <Container className="my-5">
이재연's avatar
aa    
이재연 committed
72

이재연's avatar
logsign    
이재연 committed
73
                <Row className="justify-content-center">
이재연's avatar
이재연 committed
74
                    <Col md={6} xs={10} className="border" style={{ background: '#F7F3F3' }}>
이재연's avatar
logsign    
이재연 committed
75
                        <h2 className="text-center mt-5">Sign Up</h2>
이재연's avatar
aa    
이재연 committed
76
77
78
                        {error && <Alert variant='danger'>
                            {error}
                        </Alert>}
이재연's avatar
이재연 committed
79
80
81
82
                        <Form
                            noValidate validated={validated}
                            onSubmit={handleSubmit}
                            className="p-5">
이재연's avatar
logsign    
이재연 committed
83
84
                            <Form.Group controlId="formBasicName">
                                <Form.Row>
이재연's avatar
이재연 committed
85
86
87
88
89
90
91
92
93
                                    <Col sm={4} xs={6} as={Form.Label} for="id"> </Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        required type="text"
                                        name="name"
                                        placeholder="Name"
                                        style={{ width: '160px' }}
                                        value={user.name}
                                        onChange={handleChange} />
                                    <Form.Control.Feedback type="invalid">이름을 입력하세요. </Form.Control.Feedback>
이재연's avatar
logsign    
이재연 committed
94
95
96
97
                                </Form.Row>
                            </Form.Group>
                            <Form.Group controlId="formBasicNumber">
                                <Form.Row>
이재연's avatar
이재연 committed
98
                                    <Col sm={4} xs={6} as={Form.Label} for="number">주민등록번호</Col>
이재연's avatar
이재연 committed
99
                                    <Col as={Row} sm={8} xs={10} >
100
                                            <Form.Control
이재연's avatar
이재연 committed
101
102
103
                                            required type="text"
                                            name="number1"
                                            maxlength="6"
이재연's avatar
이재연 committed
104
                                            className="mx-2" style={{ width: '17 0px' }}
이재연's avatar
이재연 committed
105
106
107
                                            value={user.number1}
                                            onChange={handleChange}>
                                        </Form.Control>
이재연's avatar
logsign    
이재연 committed
108
                                    -
이재연's avatar
이재연 committed
109
                                    <Form.Control
110
111
112
113
114
115
116
                                                required type="text"
                                                name="number2"
                                                maxlength="1" className="mx-3"
                                                style={{ width: '50px' }}
                                                value={user.number2}
                                                onChange={handleChange}>
                                            </Form.Control>
이재연's avatar
logsign    
이재연 committed
117
                                    ******
118
                                        <Form.Control.Feedback type="invalid">주민등록번호를 입력하세요.</Form.Control.Feedback>
이재연's avatar
logsign    
이재연 committed
119
120
121
                                    </Col>
                                </Form.Row>
                            </Form.Group>
이재연's avatar
이재연 committed
122
123
                            <Form.Group controlId="formBasicId">
                                <Form.Row>
이재연's avatar
이재연 committed
124
125
126
127
128
129
130
131
132
133
                                    <Col sm={4} xs={6} as={Form.Label} for="id">아이디</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        required
                                        type="text"
                                        name="id"
                                        placeholder="ID"
                                        style={{ width: '160px' }}
                                        value={user.id}
                                        onChange={handleChange} />
                                    <Form.Control.Feedback type="invalid"> 아이디를 입력하세요.</Form.Control.Feedback>
이재연's avatar
이재연 committed
134
135
                                </Form.Row>
                            </Form.Group>
이재연's avatar
logsign    
이재연 committed
136
137
                            <Form.Group controlId="formBasicPassword">
                                <Form.Row>
이재연's avatar
이재연 committed
138
139
140
141
142
143
144
145
                                    <Col sm={4} xs={6} as={Form.Label} for="password">비밀번호</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        type="password"
                                        name="password"
                                        placeholder="Password"
                                        style={{ width: '160px' }}
                                        value={user.password}
                                        required
이재연's avatar
이재연 committed
146
147
                                        onChange={handleChange}
                                    />
이재연's avatar
이재연 committed
148
149
                                    <Form.Control.Feedback className="text-center" type="invalid">
                                        비밀번호를 입력하세요.
이재연's avatar
이재연 committed
150
                                    </Form.Control.Feedback>
이재연's avatar
logsign    
이재연 committed
151
152
153
154
                                </Form.Row>
                            </Form.Group>
                            <Form.Group controlId="formBasicPassword2">
                                <Form.Row>
이재연's avatar
이재연 committed
155
156
157
158
159
160
161
162
                                    <Col sm={4} xs={6} as={Form.Label} for="password">비밀번호 확인</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        type="password"
                                        name="password2"
                                        placeholder="Password"
                                        style={{ width: '160px' }}
                                        value={user.password2}
                                        required
이재연's avatar
이재연 committed
163
                                        onChange={handleChange}
164
                                    />
이재연's avatar
이재연 committed
165
                                    <Form.Control.Feedback type="invalid"> 비밀번호를 한번  입력하세요.
이재연's avatar
이재연 committed
166
                                    </Form.Control.Feedback>
이재연's avatar
logsign    
이재연 committed
167
168
169
170
                                </Form.Row>
                            </Form.Group>
                            <Form.Group controlId="formBasicTel">
                                <Form.Row>
이재연's avatar
이재연 committed
171
172
173
174
175
176
177
178
179
                                    <Col sm={4} xs={6} as={Form.Label} for="tel">휴대전화</Col>
                                    <Col sm={8} xs={12} as={Form.Control}
                                        required
                                        type="text"
                                        name="tel"
                                        size="sm" style={{ width: '160px' }}
                                        value={user.tel}
                                        onChange={handleChange} />
                                    <Form.Control.Feedback type="invalid"> 휴대전화를 입력하세요. </Form.Control.Feedback>
이재연's avatar
logsign    
이재연 committed
180
181
                                </Form.Row>
                            </Form.Group>
이재연's avatar
이재연 committed
182
                            <Button
이재연's avatar
이재연 committed
183
184
                                style={{ background: '#91877F', borderColor: '#91877F' }} type="submit" block
                                onClick={checkPassword}
185
                            >
이재연's avatar
이재연 committed
186
187
                                Sign Up
                            </Button>
이재연's avatar
logsign    
이재연 committed
188
189
190
191
192
                        </Form>
                    </Col>
                </Row>
            </Container>
        </div>
Kim, Subin's avatar
Kim, Subin committed
193
194
195
196
    )
}

export default Signup