LogInPage.js 4.36 KB
Newer Older
우지원's avatar
01_06    
우지원 committed
1
2
import React, { useState, useEffect } from 'react';
import axios from 'axios'
Choi Ga Young's avatar
Choi Ga Young committed
3
import { Button, Form, Container, Spinner, Alert } from 'react-bootstrap';
우지원's avatar
01_06    
우지원 committed
4
import catchErrors from '../utils/catchErrors'
Choi Ga Young's avatar
Choi Ga Young committed
5
import { Redirect } from 'react-router-dom'
우지원's avatar
01_06    
우지원 committed
6
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
            setSucces(true)
        } catch (error) {
            catchErrors(error, setError)
        } finally {
            setLoading(false)
        }
        //server쪽에서 json형식으로 보낼것임
    }

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

    return (
55
        <>
우지원's avatar
우지원 committed
56
            <Menu />
Choi Ga Young's avatar
Choi Ga Young committed
57
58
59
            <Form onSubmit={handleSubmit} className="vh-90 flex-column align-items-center justify-content-center mt-2" variant="dark">
                <div className="d-flex justify-content-center">
                    <div className="mt-5 p-5 mr-2" style={{ display: "flex", flexDirection: "column", borderStyle: "solid", borderRadius: "5px", borderColor: "#4A5D7E", backgroundColor: "#FFFFFF", padding: '15px', position: "relative" }}>
60
                        <h2 className="text-center">로그인</h2>
61
62
63
64
                        <Form.Group controlId="formGroupEmail">
                            <Form.Label>이메일</Form.Label>
                            <Form.Control
                                required
우지원's avatar
a    
우지원 committed
65
                                type="email"
우지원's avatar
01_06    
우지원 committed
66
67
68
                                name="email"
                                onChange={handleChange}
                                value={user.email}
69
70
71
                                placeholder="이메일을 입력해주세요" />
                            <Form.Control.Feedback type="invalid">
                                필수 정보입니다! 이메일을 입력해주세요!
우지원's avatar
ul    
우지원 committed
72
                        </Form.Control.Feedback>
73
74
75
76
77
78
                        </Form.Group>

                        <Form.Group controlId="formGroupPassword">
                            <Form.Label>비밀번호</Form.Label>
                            <Form.Control
                                required
우지원's avatar
a    
우지원 committed
79
                                type="password"
우지원's avatar
01_06    
우지원 committed
80
81
82
                                name="password"
                                onChange={handleChange}
                                value={user.password}
83
84
85
                                placeholder="비밀번호를 입력해주세요" />
                            <Form.Control.Feedback type="invalid">
                                필수 정보입니다! 비밀번호를 입력해주세요!
우지원's avatar
ul    
우지원 committed
86
                        </Form.Control.Feedback>
87
                        </Form.Group>
우지원's avatar
01_06    
우지원 committed
88
89
90
                        <Button
                            disabled={disabled || loading}
                            type="submit"
우지원's avatar
우지원 committed
91
                            variant="outline"
우지원's avatar
01_06    
우지원 committed
92
93
                            size="lg"
                            className="mr-4"
우지원's avatar
우지원 committed
94
                            style={{ border: "3px solid", borderColor: "#b49dc9", background: 'white', font: 'dark' }}
우지원's avatar
01_06    
우지원 committed
95
                            block>
우지원's avatar
우지원 committed
96
                            {loading && <Spinner as='span' animation='border' size='sm' role='status' aria-hidden='true' style={{ color: "#b49dc9" }} />} {' '} 로그
우지원's avatar
01_06    
우지원 committed
97
98
99
100
                                </Button>
                        {error && <Alert variant='danger'>
                            {error}
                        </Alert>}
101
                    </div>
Choi Ga Young's avatar
Choi Ga Young committed
102
                </div>
103
104
            </Form>
        </>
우지원's avatar
ul    
우지원 committed
105
106
107
108
    );
}

export default LogIn