SignUp.js 5.36 KB
Newer Older
1
import React, { useState } from 'react'
Spark's avatar
Spark committed
2
3
import '../App.css'
import { Form, Modal, Button, Row, Col, Image } from 'react-bootstrap';
4
// import { KAKAO_AUTH_URL } from '../components/Oauth';
5
6
7
import { Redirect } from 'react-router-dom';
import axios from 'axios';
import catchErrors from './../Utils/CatchError';
Spark's avatar
Spark committed
8
9
10
11
12
13
14
15
16
17
18


function SignUp() {

    const inboxstyled = {
        display: 'flex',
        flexDirection: 'column',
        maxWidth: '80%',
        justifyContent: 'center',
        margin: 'auto',
        padding: '1rem'
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
    }
    function loginWithKakao() {
        window.Kakao.Auth.authorize({
            redirectUri: 'http://localhost:3000/oauth'
        })
    }

    const INIT_USER = {
        username: '',
        email: '',
        password: '',
        password2: '',
    }


    const [user, setUser] = useState(INIT_USER)
    const [error, setError] = useState('')
    const [success, setSuccess] = useState(false)
    const [validated, setValidated] = useState(false);

    function handleChange(event) {
        const { name, value } = event.target
        setUser({ ...user, [name]: value })
    }

    async function handleSubmit(event) {
        event.preventDefault()
        const form = event.currentTarget;
        // 이벤트핸들러가 부착된 것을 의미
        console.log(form)
        if (form.checkValidity() === false) {
            event.preventDefault();
            event.stopPropagation();
        }
        setValidated(true);
        try {
            setError('')
            const response = await axios.post('/api/users/signup', user)
            setSuccess(true)
        } catch (error) {
            catchErrors(error, setError)
        }
    }

    function checkPassword(event) {
        const p1 = user.password
        const p2 = user.password2
        if (p1 !== p2) {
            event.preventDefault();
            //기본적으로 정의된 이벤트 작동 제한
            event.stopPropagation();
            //부모태그로부터 이벤트의 전파 멈춤
            alert('비밀번호가 일치하지 않습니다.')
            return false
        } else {
            return true
        }
    }
Spark's avatar
Spark committed
77

78
79
80
    if (success) {
        alert('회원가입 되었습니다.')
        return <Redirect to='/user' />
Spark's avatar
Spark committed
81
82
    }

83

Spark's avatar
Spark committed
84
85
86
87
88
89
90
91
92
93
94
95
96
    return (
        <>
            <Modal.Header className='d-block text-center'>
                <Modal.Title>
                    Create an Account
                </Modal.Title>
                <p style={{ color: 'gray', margin: '10px', fontSize: '0.5em' }}>
                    Sign up with your social media account or email address
                </p>

            </Modal.Header>

            <Modal.Body>
97
98
99
100
101
                <Form style={inboxstyled}
                    noValidate validated={validated}
                    onSubmit={handleSubmit}

                >
Spark's avatar
Spark committed
102
                    <Form.Group controlId="formBasicEmail">
103
104
105
106
107
108
109
                        <Form.Control
                            type="text"
                            name="username"
                            placeholder="Username"
                            value={user.username}
                            onChange={handleChange}
                        />
Spark's avatar
Spark committed
110
111
                    </Form.Group>
                    <Form.Group controlId="formBasicEmail">
112
113
114
115
116
117
118
                        <Form.Control
                            type="email"
                            name="email"
                            placeholder="Email Address"
                            value={user.email}
                            onChange={handleChange}
                        />
Spark's avatar
Spark committed
119
                    </Form.Group>
120
121
122
123
124
125
126
127
                    <Form.Group controlId="formBsicPassword">
                        <Form.Control
                            type="password"
                            name="password"
                            placeholder="Password"
                            value={user.password}
                            onChange={handleChange}
                        />
Spark's avatar
Spark committed
128
129
                    </Form.Group>
                    <Form.Group controlId="formBasicPassword">
130
131
132
133
134
135
136
                        <Form.Control
                            type="password"
                            name="password2"
                            placeholder="Confirm Password"
                            value={user.password2}
                            onChange={handleChange}
                        />
Spark's avatar
Spark committed
137
138
                    </Form.Group>

139
                    <Button variant='light' type="submit" id='formbtn' onClick={checkPassword}>
Spark's avatar
Spark committed
140
141
142
143
                        Sign Up
                    </Button>
                </Form>

144
145
146



Spark's avatar
Spark committed
147
                <hr />
148
149
150
151
152
153
154
155
156
157
158
159
160
                <Row style={{ margin: 'auto', marginBottom: '5px', display: 'flex', justifyContent: 'center' }}>
                    <a href="#" onClick={loginWithKakao} id='socialLink' >
                        <img src='/images/Kakao1.jpg' id='logpng' />
                        KAKAO
                    </a>
                    <a href="javascript:loginWithKakao()" id='socialLink'>
                        <Image src='/images/google.png' id='logpng' />
                        GOOGLE
                    </a>
                    <a href="javascript:loginWithKakao()" id='socialLink'>
                        <Image src='/images/facebook.png' id='logpng' />
                        FACEBOOK
                    </a>
Spark's avatar
Spark committed
161
162
163
164
165
166
167
168
                </Row>
            </Modal.Body>

        </>
    )
}

export default SignUp;