LoginPage.js 5.21 KB
Newer Older
CHAERIN KIM's avatar
CHAERIN KIM committed
1
import React, { useState } from 'react';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
2
import styled from 'styled-components';
CHAERIN KIM's avatar
CHAERIN KIM committed
3
import { Link, Redirect } from 'react-router-dom';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
4
5
import { Formik } from 'formik';
import * as Yup from 'yup';
CHAERIN KIM's avatar
CHAERIN KIM committed
6
7
import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.css';
Ha YeaJin's avatar
pages    
Ha YeaJin committed
8
9
10
11
12
13
14
15
16
17

const Log = styled.div`
    background-color: #981e1e;
`
const Logo = styled.div`
    background-color: #E76A6A;
`


function Login() {
CHAERIN KIM's avatar
CHAERIN KIM committed
18
19
20
21
22
    const [state, setState]= useState(false);

  if (state) {
    return <Redirect to="/home" />;
  }
Ha YeaJin's avatar
pages    
Ha YeaJin committed
23
24
25
26
27
28
29
30
31
    return (
        <div className="container-fluid">
            <div className="row">
                <Logo className="col-md-5 col-12">
                    <h2>고려대학교</h2>
                    <h4>대관 시스템</h4>
                </Logo>
                <Log className="col-md-7 col-12">
                    <Formik
CHAERIN KIM's avatar
CHAERIN KIM committed
32
                        initialValues={{ id: '', password: '' }}
Ha YeaJin's avatar
pages    
Ha YeaJin committed
33
                        validationSchema={Yup.object({
CHAERIN KIM's avatar
CHAERIN KIM committed
34
35
                            id: Yup.string()
                        .required('아이디를 입력해주세요.'),
Ha YeaJin's avatar
pages    
Ha YeaJin committed
36
37
38
39
40
                            password: Yup.string()
                                .required('비밀번호를 입력해주세요.')
                                .min(8, '8자 이상 입력해주세요.'),
                        })}
                        onSubmit={(values, { setSubmitting }) => {
CHAERIN KIM's avatar
CHAERIN KIM committed
41
42
43
44
45
46
                            axios({
                              method: 'post',
                              url: '/login',
                              data: values,
                            }).then(res => {
                              if (res.status === 404) return alert(res.data.error)
Ha YeaJin's avatar
pages    
Ha YeaJin committed
47
48
                            alert("로그인이 완료되었습니다!")

CHAERIN KIM's avatar
CHAERIN KIM committed
49
50
51
52
53
54
55
                              localStorage.setItem('token', res.data.token);
                              localStorage.setItem('id', res.data.users._id);
                              setState(true);
                            })
                              .catch(err => {
                                alert(err.error)
                              });
Ha YeaJin's avatar
pages    
Ha YeaJin committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

                            setTimeout(() => {
                                setSubmitting(false);
                            }, 400);  // finish the cycle in handler
                        }}
                    >
                        {({
                            errors,
                            touched,
                            handleSubmit,
                            getFieldProps,  // contain values, handleChange, handleBlur
                            isSubmitting,
                        }) => (
                                <div className="row justify-content-center align-items-center">
                                    <form onSubmit={handleSubmit} className="col-sm-3">
                                        <div className="form-group mb-4">
                                            <input
CHAERIN KIM's avatar
CHAERIN KIM committed
73
74
75
76
77
                                                className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
                                                type="text"
                                                name="id"
                                                {...getFieldProps('id')}
                                                placeholder="Input id"
Ha YeaJin's avatar
pages    
Ha YeaJin committed
78
                                            />
CHAERIN KIM's avatar
CHAERIN KIM committed
79
80
                                            {touched.id && errors.id ? (
                                                <div className="invalid-feedback text-left">{errors.id}</div>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
                                            ) : null}
                                        </div>
                                        <div className="form-group mb-4">
                                            <input
                                                className={(touched.password && errors.password ? 'form-control is-invalid' : "form-control")}
                                                type="password"
                                                name="password"
                                                {...getFieldProps('password')}
                                                placeholder="Input Password"
                                            />
                                            {touched.password && errors.password ? (
                                                <div className="invalid-feedback text-left">{errors.password}</div>
                                            ) : null}
                                        </div>
                                        <button type="submit" className="btn btn-dark" disabled={isSubmitting}>
                                            Login
CHAERIN KIM's avatar
CHAERIN KIM committed
97
98
                                        </button>
                                        <button><Link to="/home"></Link></button>
Ha YeaJin's avatar
pages    
Ha YeaJin committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                                        <Link to="/signup">비밀번호를 잊으셨나요?</Link>
                                        <div></div>
                                        <Link to="/signup">회원이 아니신가요?</Link>
                                    </form>
                                </div>
                            )}
                    </Formik>

                </Log>
            </div>
        </div>
    )
}

export default Login