Login.js 3.13 KB
Newer Older
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
1
import React, { useState, useEffect } from 'react'
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
2
import { Alert, Col, Container, Form, Row, Button, Spinner, Image } from "react-bootstrap"
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
3
4
5
6
import axios from "axios"
import catchErrors from '../utils/catchErrors'
import { Redirect } from 'react-router-dom'
import { handleLogin } from '../utils/auth'
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
7
8
import { Link } from 'react-router-dom';
import ohuh from '../ohuh-sm.PNG';
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
9
10
11
12

const INIT_USER = {
    email: '',
    password: ''
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
13
}  //초기 유저에 이메일 비밀번호 설정
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
14
15

function Login() {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
16
    //const [<상태 값 저장 변수>, <상태 값 갱신 함수>] = useState(<상태 초기 값>);
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
17
18
19
20
21
22
    const [user, setUser] = useState(INIT_USER)
    const [disabled, setDisabled] = useState(true)
    const [error, setError] = useState('')
    const [success, setSuccess] = useState(false)
    const [loading, setLoading] = useState(false)

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
23
    useEffect(() => { //참거짓 판단 값들의 원소들이 element로 들어간다 
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
24
        const isUser = Object.values(user).every(el => Boolean(el))
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
25
        isUser ? setDisabled(false) : setDisabled(true) 
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
26
27
28
    }, [user])

    function handleChange(event) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
29
30
        const {name, value} = event.target //{}안에 값을 이벤트.타겟에 지정?한다
        setUser({...user, [name]: value}) 
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
31
32
33
    }

    async function handleSubmit(event) {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
34
        event.preventDefault() //리셋을 막는다
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
35
36
        try {
            setLoading(true)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
37
            setError('') //값을 배열로 설정
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
38
39
            const response = await axios.post('/api/auth/login', user)
            console.log(response.data)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
40
            handleLogin(response.data.userId)//로그인했을때 userid data를 넣는다
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
41
42
43
44
45
46
47
48
49
            setSuccess(true)
        } catch (error) {
            catchErrors(error, setError)
        } finally {
            setLoading(false)
        }
    }

    if (success) {
baesangjune's avatar
baesangjune committed
50
        alert("로그인되었습니다.")
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
51
        return <Redirect to= '/'/> //성공하면 홈화면으로 간다
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
52
53
54
55
56
    }

    return (
        <Container>
            <Row className= 'vh-100 flex-column align-items-center justify-content-center'>
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
57
            <Link to='/'><Image src={ohuh}/></Link>
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
            <h2>로그인</h2>
                <Col md={6}>
            {error && <Alert variant='danger'>
                {error}
            </Alert>}
                <Form onSubmit={handleSubmit}>
                    <Form.Group>
                        <Form.Label>이메일</Form.Label>
                        <Form.Control name='email' type='email'value={user.email} onChange={handleChange}/>
                    </Form.Group>
                    <Form.Group>
                        <Form.Label>비밀번호</Form.Label>
                        <Form.Control name='password' type='password' value={user.password} onChange={handleChange}/>
                    </Form.Group>
                    <Button disabled={disabled || setLoading} type='submit' 
                    block>
                        {loading && <Spinner as= 'span' animation= 'border' size='sm' 
                        role= 'status' aria-hidden=  'true' />}{' '}확인</Button>
                </Form>
                </Col>
            </Row>
        </Container>
    )
}

export default Login