Login.js 4.59 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import React, { useState, useEffect, useRef } from 'react';
2
import { Link, Redirect } from 'react-router-dom';
Kim, Subin's avatar
Kim, Subin committed
3
4
import Nav1 from '../Components/MainNav';
import Nav2 from '../Components/SubNav';
이재연's avatar
이재연 committed
5
6
7
8
import { Form, Col, Container, Button, Row, Alert } from 'react-bootstrap';
import axios from 'axios'
import catchErrors from '../utils/catchErrors'
import { handleLogin } from '../utils/auth'
이재연's avatar
logsign    
이재연 committed
9

이재연's avatar
이재연 committed
10

이재연's avatar
이재연 committed
11
12
13
14
15
16
const INIT_USER = {
    id: '',
    password: ''
}

function Login() {
이재연's avatar
이재연 committed
17

이재연's avatar
이재연 committed
18
19
20
21
22
23
24
25
26
27
28
    const [validated, setValidated] = useState(false);
    const [user, setUser] = useState(INIT_USER)
    const [error, setError] = useState('')
    const [success, setSuccess] = useState(false)

    async function handleSubmit(event) {
        event.preventDefault()
        const form = event.currentTarget;
        if (form.checkValidity() === false) {
            event.preventDefault();
            event.stopPropagation();
이재연's avatar
이재연 committed
29
30
        }
        setValidated(true);
이재연's avatar
이재연 committed
31
32
33
34
35
36
37
38
39
40
41
42
        try {
            setError('')
            await axios.post('api/auth/login', user)
            handleLogin()
            setSuccess(true)
        } catch (error) {

            catchErrors(error, setError)
        }
        if (success) {
            return <Redirect to='/' />
        }
이재연's avatar
이재연 committed
43
    }
이재연's avatar
이재연 committed
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


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

        }



        return (
            <div>
                <Nav1 />
                <Nav2 />
                <Container className="my-5">
                    <Row className="justify-content-center">
                        <Col md={5} xs={10} className="border" style={{ background: '#F7F3F3' }}>
                            <h3 className="text-center mt-5">Login</h3>
                            {error && <Alert variant='danger'>
                                {error}
                            </Alert>}
                            <Form noValidate validated={validated}
                                onSubmit={handleSubmit}
                                className="p-5">
                                <Form.Group controlId="formBasicId">
                                    <Form.Row>
                                        <Col sm={4} xs={6} as={Form.Label} for="id"> 아이디</Col>
이재연's avatar
이재연 committed
71
72
                                        <Col sm={8} xs={12} as={Form.Control}
                                            required
이재연's avatar
이재연 committed
73
                                            type="text"
이재연's avatar
이재연 committed
74
                                            name="id"
이재연's avatar
이재연 committed
75
                                            placeholder="ID"
이재연's avatar
이재연 committed
76
77
                                            value={user.id}
                                            onChange={handleChange}
이재연's avatar
이재연 committed
78
79
80
                                            style={{ width: '160px' }}>
                                        </Col>
                                        <Form.Control.Feedback className="text-center" type="invalid"> 아이디를 입력하세요.</Form.Control.Feedback>
이재연's avatar
이재연 committed
81
82
                                    </Form.Row>
                                </Form.Group>
이재연's avatar
logsign    
이재연 committed
83

이재연's avatar
이재연 committed
84
85
86
                                <Form.Group controlId="formBasicPassword">
                                    <Form.Row>
                                        <Col sm={4} xs={6} as={Form.Label} for="password">비밀번호</Col>
이재연's avatar
이재연 committed
87
                                        <Col sm={8} xs={12} as={Form.Control}
이재연's avatar
이재연 committed
88
                                            type="password"
이재연's avatar
이재연 committed
89
90
                                            name="password"
                                            value={user.password}
이재연's avatar
이재연 committed
91
                                            placeholder="Password"
이재연's avatar
이재연 committed
92
                                            onChange={handleChange}
이재연's avatar
이재연 committed
93
94
                                            style={{ width: '160px' }}
                                            required />
이재연's avatar
이재연 committed
95
96
                                        <Form.Control.Feedback className="text-center" type="invalid">
                                            비밀번호를 입력하세요.
이재연's avatar
이재연 committed
97
                                    </Form.Control.Feedback>
이재연's avatar
이재연 committed
98
99
100
101
102
103
104
105
106
107
108
109
110
                                    </Form.Row>
                                </Form.Group>
                                <Button style={{ background: '#91877F', borderColor: '#91877F' }} type="submit" block>Login</Button>
                                <div className="loginLine">
                                    <Link to="/signup" style={{ color: '#91877F' }}>회원이 아니십니까?</Link>
                                </div>
                            </Form>
                        </Col>
                    </Row>
                </Container>
            </div>
        )
    
Kim, Subin's avatar
Kim, Subin committed
111
112
113
}

export default Login