Login.tsx 2.56 KB
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
import React, { ChangeEvent, FormEvent, useState } from "react";
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
2
import { useNavigate } from "react-router-dom";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
4
5
import { catchErrors } from "../helpers";
import { SpinnerIcon } from "../icons";
import { useAuth } from "./auth.context";
6

Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
7
export const Login = () => {
Yoon, Daeki's avatar
Yoon, Daeki committed
8
9
10
11
12
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [loginData, setLoginData] = useState({ email: "", password: "" });
  const navigate = useNavigate();
  const { login } = useAuth();
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
13

Yoon, Daeki's avatar
Yoon, Daeki committed
14
15
16
17
  function handleChange(e: ChangeEvent<HTMLInputElement>) {
    const { name, value } = e.currentTarget;
    setLoginData({ ...loginData, [name]: value });
  }
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
18

Yoon, Daeki's avatar
Yoon, Daeki committed
19
20
21
22
23
24
25
26
27
28
29
30
  async function handleSubmit(e: FormEvent) {
    e.preventDefault();
    console.log(loginData);
    const { email, password } = loginData;
    try {
      setLoading(true);
      await login(email, password, () => navigate("/", { replace: true }));
    } catch (error) {
      setLoading(false);
      catchErrors(error, setError);
    }
  }
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
31
32
33

  return (
    <div className="flex justify-center mt-3">
Yoon, Daeki's avatar
Yoon, Daeki committed
34
35
36
37
      <form
        onSubmit={handleSubmit}
        className="flex flex-col space-y-4 mt-5 text-xl font-bold"
      >
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
38
39
40
41
        <label className="block text-gray-700 text-sm font-bold mb-2 mt-3">
          이메일
        </label>
        <input
Yoon, Daeki's avatar
Yoon, Daeki committed
42
          onChange={handleChange}
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
43
          className="shadow appearance-none border rounded  py-2 px-3 text-gray-70"
Yoon, Daeki's avatar
Yoon, Daeki committed
44
          name="email"
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
45
          type="email"
Yoon, Daeki's avatar
Yoon, Daeki committed
46
          autoComplete="username"
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
47
          placeholder="이메일을 입력하세요"
Yoon, Daeki's avatar
Yoon, Daeki committed
48
          value={loginData.email}
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
49
50
51
52
53
54
        />

        <label className="block text-gray-700 text-sm font-bold mb-2 mt-3">
          비밀번호
        </label>
        <input
Yoon, Daeki's avatar
Yoon, Daeki committed
55
          onChange={handleChange}
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
56
          className="shadow appearance-none border rounded py-2 px-3 text-gray-70"
Yoon, Daeki's avatar
Yoon, Daeki committed
57
          name="password"
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
58
          type="password"
Yoon, Daeki's avatar
Yoon, Daeki committed
59
          autoComplete="current-password"
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
60
          placeholder="비밀번호를 입력하세요"
Yoon, Daeki's avatar
Yoon, Daeki committed
61
          value={loginData.password}
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
62
        />
Yoon, Daeki's avatar
Yoon, Daeki committed
63
64
65
66
67
        {error && (
          <div className="text-red-500 text-sm mb-6">
            <p>{error}</p>
          </div>
        )}
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
68
        <div className="text-center">
Yoon, Daeki's avatar
Yoon, Daeki committed
69
70
71
72
73
74
75
76
          <button
            type="submit"
            disabled={loading ? true : false}
            className="bg-themeColor text-white border rounded w-100 py-2 px-3 mt-3"
          >
            {loading && (
              <SpinnerIcon className="animate-spin h-5 w-5 mr-1 text-white" />
            )}
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
77
78
79
            로그인
          </button>
        </div>
Yoon, Daeki's avatar
Yoon, Daeki committed
80
      </form>
Jiwon Yoon's avatar
Jiwon Yoon committed
81
    </div>
Lee SeoYeon's avatar
0704    
Lee SeoYeon committed
82
83
  );
};