LoginForm.js 2.58 KB
Newer Older
1
import { useState } from 'react';
2
import { Redirect, Link } from "react-router-dom";
3
4
import { Formik } from 'formik';
import * as Yup from 'yup';
5
6
import authApi from '../../apis/auth.api';
import catchErrors from "../../utils/catchErrors.js";
7
8

const LoginForm = () => {
9
10
11
12
13
14
  const [success, setSuccess] = useState(false);
  const [error, setError] = useState("");

  if (success) {
    return <Redirect to="/home" />;
  }
15
  return (
Kim, Subin's avatar
Kim, Subin committed
16
    <>
17
18
19
20
21
22
23
24
25
26
27
      <Formik
        initialValues={{
          userId: '',
          password: '',
        }}
        validationSchema={Yup.object({
          userId: Yup.string()
            .required('아이디를 입력해주세요.'),
          password: Yup.string()
            .required('비밀번호를 입력해주세요.'),
        })}
Choi Ga Young's avatar
Choi Ga Young committed
28
        onSubmit={async (values, { setSubmitting, resetForm }) => {
29
30
31
32
33
34
35
36
          try {
            setError("")
            const result = await authApi.login(values)
            if (result.status === 201) {
              setSuccess(true)
            }
          } catch (error) {
            catchErrors(error, setError)
Choi Ga Young's avatar
Choi Ga Young committed
37
            resetForm();
38
          }
39
40
41
42
43
44
          setTimeout(() => {
            setSubmitting(false);
          }, 400);
        }}
      >
        {formik => (
Kim, Subin's avatar
Kim, Subin committed
45
46
          <form onSubmit={formik.handleSubmit} className="m-5">
            <div className="mb-3">
47
              <input type="text" name="userId"
Kim, Subin's avatar
Kim, Subin committed
48
                className="form-control border-top-0 border-end-0 border-start-0 shadow-none rounded-0"
49
50
51
                placeholder="아이디"
                {...formik.getFieldProps('userId')} />
              {formik.touched.userId && formik.errors.userId ? (
52
                <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.userId}</div>
53
54
55
56
              ) : null}
            </div>
            <div className="mb-3">
              <input type="password" name="password"
Kim, Subin's avatar
Kim, Subin committed
57
                className="form-control border-top-0 border-end-0 border-start-0 shadow-none rounded-0"
58
59
60
                placeholder="비밀번호"
                {...formik.getFieldProps('password')} />
              {formik.touched.password && formik.errors.password ? (
61
                <div className="text-danger mt-1" style={{ fontSize: "10px" }}>{formik.errors.password}</div>
62
63
64
65
66
67
68
69
70
              ) : null}
            </div>
            <div className="d-grid gap-2 mt-5">
              <button type="submit" className="btn btn-crimson">로그인</button>
              <Link className="btn btn-crimson" to="/signup">회원가입</Link>
            </div>
          </form>
        )}
      </Formik>
Kim, Subin's avatar
Kim, Subin committed
71
    </>
72
73
74
75
76
  );
}

export default LoginForm;