login.tsx 3.46 KB
Newer Older
1
2
3
4
5
import { Link, useNavigate } from "react-router-dom";
import React, { useState, useEffect, FormEvent } from "react";
import { LoginUser } from "../types";
import { catchErrors } from "../helpers";
import { useAuth } from "./auth.context";
Kim, MinGyu's avatar
처음    
Kim, MinGyu committed
6

7
8
9
10
11
12
13
14
15
16
17
18
export default function Login() {
  const [user, setUser] = useState<LoginUser>({
    email: "",
    password: "",
  });

  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [disabled, setDisabled] = useState(false);
  const [success, setSuccess] = useState(false);
  const navigate = useNavigate();
  const { login } = useAuth();
Kim, MinGyu's avatar
signup    
Kim, MinGyu committed
19

20
21
22
  useEffect(() => {
    setDisabled(!(user.email && user.password));
  }, [user]);
Kim, MinGyu's avatar
Kim, MinGyu committed
23

24
25
26
27
  function handleChange(event: React.ChangeEvent<HTMLInputElement>) {
    const { name, value } = event.currentTarget;
    setUser({ ...user, [name]: value });
  }
Kim, MinGyu's avatar
Kim, MinGyu committed
28

29
30
31
32
33
  async function handleSubmit(event: FormEvent) {
    event.preventDefault();
    try {
      setError("");
      console.log("user data", user);
Kim, MinGyu's avatar
Kim, MinGyu committed
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      // setLoading(true);
      await login(user.email, user.password, () => {
        navigate("/", { replace: true });
      });
      // console.log("서버연결됬나요", res);
      // console.log("로그인");
      // setSuccess(true);
      // setError("");
    } catch (error) {
      console.log("에러발생");
      // setError("이메일 혹은 비밀번호를 다시 입력해주세요.");
      catchErrors(error, setError);
    } finally {
      setLoading(false);
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
50
  }
51

Kim, MinGyu's avatar
Kim, MinGyu committed
52
  return (
53
54
55
56
    <div className="flex flex-col items-center my-10">
      <div className="bg-white w-1/2 md:w-1/3 my-8 text-center text-2xl">
        <Link to="/">Travel Report</Link>
      </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
57
      <div className="flex flex-col w-full md:w-1/3 p-8 md:p-4 md:p-0">
58
59
60
61
62
63
        <form onSubmit={handleSubmit}>
          <div className="flex flex-col md:flex-row border-2 border-black rounded-xl p-8 md:p-12 gap-y-4 md:gap-x-6">
            <div className="flex flex-col md:w-2/3 md:gap-2 ">
              <input
                className="placeholder:text-slate-300
                bg-white border border-slate-500 md:rounded-2xl
Kim, MinGyu's avatar
Kim, MinGyu committed
64
                my-2 py-3 md:py-2 pl-9 pr-3 
Kim, MinGyu's avatar
Kim, MinGyu committed
65
66
                focus:border-black
                "
67
68
69
70
71
72
73
74
75
76
                placeholder="이메일"
                type="email"
                name="email"
                onChange={handleChange}
              />

              <input
                className="placeholder:italic placeholder:text-slate-300
                bg-white border border-slate-500 md:rounded-2xl
                py-3 md:py-2 pl-9 pr-3 
Kim, MinGyu's avatar
Kim, MinGyu committed
77
78
                focus:border-black
                "
79
80
81
82
83
84
85
86
87
88
89
90
                placeholder="Password"
                type="password"
                name="password"
                onChange={handleChange}
              />
            </div>
            <button
              disabled={disabled}
              type="submit"
              className="my-4 md:my-0 md:w-1/3 bg-sky-600 hover:bg-sky-700 rounded-xl text-xl py-4"
            >
              login
Kim, MinGyu's avatar
Kim, MinGyu committed
91
            </button>
92
93
94
95
96
97
98
99
          </div>
        </form>
        <div className="flex justify-around m-4">
          <button className="bg-white ">
            <Link to="/signup">회원가입</Link>
          </button>
          <div className="grid grid-cols-2 divide-x-2">
            <div></div>
Kim, MinGyu's avatar
Kim, MinGyu committed
100
101
            <div></div>
          </div>
102
103
104
          <button className="bg-white">
            <Link to="/forgot">비밀번호 찾기</Link>
          </button>
Kim, MinGyu's avatar
Kim, MinGyu committed
105
106
        </div>
      </div>
107
    </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
108
109
  );
}