LogInPage.js 4.15 KB
Newer Older
우지원's avatar
01_06    
우지원 committed
1
2
3
4
5
6
import React, { useState, useEffect } from 'react';
import axios from 'axios'
import { Button, Form, Container, Navbar, Spinner, Alert } from 'react-bootstrap';
import catchErrors from '../utils/catchErrors'
import { Link, Redirect } from 'react-router-dom'
import { handleLogin } from '../utils/auth'
우지원's avatar
우지원 committed
7
import Menu from '../Components/Menu';
우지원's avatar
01_06    
우지원 committed
8
9
10
11
12

const INIT_USER = {
    email: '',
    password: '',
}
우지원's avatar
ul    
우지원 committed
13
14

function LogIn() {
우지원's avatar
01_06    
우지원 committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    const [user, setUser] = useState(INIT_USER)
    const [disabled, setDisabled] = useState(true)
    const [error, setError] = useState('')
    const [success, setSucces] = useState(false)
    const [loading, setLoading] = useState(false)

    useEffect(() => {
        const isUser = Object.values(user).every(el => Boolean(el))
        isUser ? setDisabled(false) : setDisabled(true)
    }, [user])

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

    async function handleSubmit(event) {
        event.preventDefault()
        try {
            setLoading(true)
            setError('')
36
37
            let res = await axios.post('/auth/login', user)
            handleLogin(res.data)
우지원's avatar
01_06    
우지원 committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            setSucces(true)
        } catch (error) {
            catchErrors(error, setError)
            //setError(error.response.data)
            //error객체가 들어감. 
        } finally {
            setLoading(false)
        }
        //server쪽에서 json형식으로 보낼것임
    }

    //success시 링크이동
    if (success) {
        console.log('success', success)
우지원's avatar
우지원 committed
52
        alert('로그인 되었습니다!')
우지원's avatar
01_06    
우지원 committed
53
54
        return <Redirect to='/' />
    }
우지원's avatar
ul    
우지원 committed
55
56

    return (
57
        <>
우지원's avatar
우지원 committed
58
            <Menu />
우지원's avatar
우지원 committed
59
            <Form onSubmit={handleSubmit} className='vh-100 flex-column align-items-center justify-content-center mt-2'>
60
61
                <Container className="d-flex justify-content-center">
                    <div className="mt-5 p-5 shadow w-75">
62
                        <h2 className="text-center">로그인</h2>
63
64
65
66
                        <Form.Group controlId="formGroupEmail">
                            <Form.Label>이메일</Form.Label>
                            <Form.Control
                                required
우지원's avatar
a    
우지원 committed
67
                                type="email"
우지원's avatar
01_06    
우지원 committed
68
69
70
                                name="email"
                                onChange={handleChange}
                                value={user.email}
71
72
73
                                placeholder="이메일을 입력해주세요" />
                            <Form.Control.Feedback type="invalid">
                                필수 정보입니다! 이메일을 입력해주세요!
우지원's avatar
ul    
우지원 committed
74
                        </Form.Control.Feedback>
75
76
77
78
79
80
                        </Form.Group>

                        <Form.Group controlId="formGroupPassword">
                            <Form.Label>비밀번호</Form.Label>
                            <Form.Control
                                required
우지원's avatar
a    
우지원 committed
81
                                type="password"
우지원's avatar
01_06    
우지원 committed
82
83
84
                                name="password"
                                onChange={handleChange}
                                value={user.password}
85
86
87
                                placeholder="비밀번호를 입력해주세요" />
                            <Form.Control.Feedback type="invalid">
                                필수 정보입니다! 비밀번호를 입력해주세요!
우지원's avatar
ul    
우지원 committed
88
                        </Form.Control.Feedback>
89
90
                        </Form.Group>

우지원's avatar
01_06    
우지원 committed
91
92
93
94
95
96
97
98
99
100
101
102
                        <Button
                            disabled={disabled || loading}
                            type="submit"
                            variant="outline-success"
                            size="lg"
                            className="mr-4"
                            block>
                            {loading && <Spinner as='span' animation='border' size='sm' role='status' aria-hidden='true' />} {' '} 로그인
                                </Button>
                        {error && <Alert variant='danger'>
                            {error}
                        </Alert>}
103
104
105
106
                    </div>
                </Container>
            </Form>
        </>
우지원's avatar
ul    
우지원 committed
107
108
109
110
    );
}

export default LogIn