LogInPage.js 5.07 KB
Newer Older
우지원's avatar
01_06    
우지원 committed
1
2
3
4
5
6
7
8
9
10
11
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'

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

function LogIn() {
    const [validated, setValidated] = useState(false);
우지원'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
36
37
    const [user, setUser] = useState(INIT_USER)
    //로딩, 에러, diserved 상태 넣어야됨.
    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))
        //Boolean : 참거짓 판별
        //every : every뒤에 함수값이 return하는 값이 모두 참일때만 true출력 -> element가 하나도 빈 문자열이 존재하지 않을때
        //empty string때만 false로 나옴.
        isUser ? setDisabled(false) : setDisabled(true)
    }, [user])

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

    async function handleSubmit(event) {
        event.preventDefault()
우지원's avatar
ul    
우지원 committed
38

39
40
41
42
43
44
        // const form = event.currentTarget;
        // if (form.checkValidity() === false) {
        //     event.preventDefault();
        //     event.stopPropagation();
        // }
        // setValidated(true);
우지원's avatar
01_06    
우지원 committed
45
46
47
48
49
50

        try {
            setLoading(true)
            setError('')
            await axios.post('/auth/login', user)
            // 알아서 stringify하기 때문에 따로 해줄 필요 없음.
51
            handleLogin(user)
우지원's avatar
01_06    
우지원 committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
            setSucces(true)
        } catch (error) {
            catchErrors(error, setError)
            //setError(error.response.data)
            //error객체가 들어감. 
        } finally {
            setLoading(false)
        }
        //server쪽에서 json형식으로 보낼것임
    }

    //success시 링크이동
    if (success) {
        console.log('success', success)
        return <Redirect to='/' />
    }
우지원's avatar
ul    
우지원 committed
68
69

    return (
70
        <>
우지원's avatar
01_06    
우지원 committed
71
72
73
            {/* <Navbar bg="dark" variant="dark">
                    <Navbar.Brand>YDK Messenger</Navbar.Brand>
                </Navbar> */}
우지원's avatar
병합    
우지원 committed
74

우지원's avatar
01_06    
우지원 committed
75
            <Form noValidate validated={validated} onSubmit={handleSubmit} className='vh-100 flex-column align-items-center justify-content-center mt-2'>
76
77
78
                <Container className="d-flex justify-content-center">
                    <div className="mt-5 p-5 shadow w-75">

79
                        <h2 className="text-center">로그인</h2>
80
81
82
83
84

                        <Form.Group controlId="formGroupEmail">
                            <Form.Label>이메일</Form.Label>
                            <Form.Control
                                required
우지원's avatar
a    
우지원 committed
85
                                type="email"
우지원's avatar
01_06    
우지원 committed
86
87
88
                                name="email"
                                onChange={handleChange}
                                value={user.email}
89
90
91
                                placeholder="이메일을 입력해주세요" />
                            <Form.Control.Feedback type="invalid">
                                필수 정보입니다! 이메일을 입력해주세요!
우지원's avatar
ul    
우지원 committed
92
                        </Form.Control.Feedback>
93
94
95
96
97
98
                        </Form.Group>

                        <Form.Group controlId="formGroupPassword">
                            <Form.Label>비밀번호</Form.Label>
                            <Form.Control
                                required
우지원's avatar
a    
우지원 committed
99
                                type="password"
우지원's avatar
01_06    
우지원 committed
100
101
102
                                name="password"
                                onChange={handleChange}
                                value={user.password}
103
104
105
                                placeholder="비밀번호를 입력해주세요" />
                            <Form.Control.Feedback type="invalid">
                                필수 정보입니다! 비밀번호를 입력해주세요!
우지원's avatar
ul    
우지원 committed
106
                        </Form.Control.Feedback>
107
108
                        </Form.Group>

우지원's avatar
01_06    
우지원 committed
109
110
111
112
113
114
115
116
117
                        <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>
118

우지원's avatar
a    
우지원 committed
119
                        <Link to="./signup">
120
121
                            <h6 type="button" className="text-right mt-2" style={{ cursor: 'pointer' }}>회원가입</h6>
                        </Link>
우지원's avatar
01_06    
우지원 committed
122
123
124
                        {error && <Alert variant='danger'>
                            {error}
                        </Alert>}
125
126
127
128
                    </div>
                </Container>
            </Form>
        </>
우지원's avatar
ul    
우지원 committed
129
130
131
132
    );
}

export default LogIn