Login.js 2.91 KB
Newer Older
1
2
import { useEffect, useState} from 'react'
import { Link } from 'react-router-dom'
seoyeon's avatar
0726    
seoyeon committed
3
import userApi from '../apis/user.api'
seoyeon's avatar
useRef    
seoyeon committed
4
5
import catchErrors from '../context/catchError'
import { handleLogin } from '../context/auth'
Kim, Chaerin's avatar
Kim, Chaerin committed
6
const INIT_USER = {
우지원's avatar
0802    
우지원 committed
7
  userId: '9999',
seoyeon's avatar
0726    
seoyeon committed
8
9
10
  email: '',
  password: '',
}
Kim, Chaerin's avatar
Kim, Chaerin committed
11

Kim, Chaerin's avatar
Kim, Chaerin committed
12
const Login = () => {
seoyeon's avatar
0726    
seoyeon committed
13
14
15
16
  const [user, setUser] = useState(INIT_USER)
  const [disabled, setDisabled] = useState(true)
  const [error, setError] = useState('')
  const [success, setSuccess] = useState(false)
우지원's avatar
우지원 committed
17
  const [id, setId] = useState('')
Kim, Chaerin's avatar
Kim, Chaerin committed
18
19

  useEffect(() => {
seoyeon's avatar
0726    
seoyeon committed
20
21
22
    const isUser = Object.values(user).every((el) => Boolean(el))
    isUser ? setDisabled(false) : setDisabled(true)
  }, [user])
Kim, Chaerin's avatar
Kim, Chaerin committed
23
24

  function handleChange(event) {
seoyeon's avatar
0726    
seoyeon committed
25
26
    const { name, value } = event.target
    setUser({ ...user, [name]: value })
Kim, Chaerin's avatar
Kim, Chaerin committed
27
28
  }

Kim, Chaerin's avatar
.    
Kim, Chaerin committed
29
  async function handleSubmit(e) {
seoyeon's avatar
0726    
seoyeon committed
30
    e.preventDefault()
Kim, Chaerin's avatar
Kim, Chaerin committed
31
32
33
    try {
      // setLoading(true);
      // setError("");
seoyeon's avatar
0726    
seoyeon committed
34
35
      const data = await userApi.login(user)
      console.log(data)
우지원's avatar
우지원 committed
36
      setId(data.id)
seoyeon's avatar
seoyeon committed
37
      handleLogin(data.id)
seoyeon's avatar
0726    
seoyeon committed
38
      setSuccess(true)
Kim, Chaerin's avatar
Kim, Chaerin committed
39
    } catch (error) {
seoyeon's avatar
0726    
seoyeon committed
40
41
      console.log('error in login', error)
      catchErrors(error, setError)
Kim, Chaerin's avatar
Kim, Chaerin committed
42
43
44
45
    } finally {
      // setLoading(false);
    }
  }
seoyeon's avatar
0716    
seoyeon committed
46
  if (success) {
이재연's avatar
이재연 committed
47
    alert('로그인 되었습니다');
48
    window.location.href=`/user/${id}`
seoyeon's avatar
0716    
seoyeon committed
49
50
  }

seoyeon's avatar
0726    
seoyeon committed
51
  const { email, password } = user
Kim, Chaerin's avatar
Kim, Chaerin committed
52
53

  return (
seoyeon's avatar
seoyeon committed
54
    <div className="container">
Kim, Chaerin's avatar
Kim, Chaerin committed
55
      <form onSubmit={handleSubmit}>
seoyeon's avatar
seoyeon committed
56
57
58
59
        <div className="m-3 d-flex justify-content-center">
          <Link to="/">
            <img src="/BORA.png" style={{ width: '160px' }} />
          </Link>
Kim, Chaerin's avatar
Kim, Chaerin committed
60
        </div>
seoyeon's avatar
seoyeon committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        <div className="row mt-5 d-flex align-items-center">
          <h2 style={{ textAlign: 'center' }}>로그인</h2>
          {error && <div className="alert alert-danger">{error}</div>}
          <div className="form-group">
            <div className="mt-5">
              <label>아이디</label>
              <input
                className="form-control"
                id="email"
                type="text"
                name="email"
                placeholder="아이디를 입력하세요"
                value={email}
                onChange={handleChange}
              />
            </div>
            <div className="mt-3">
              <label>비밀번호</label>
              <input
                className="form-control"
                id="password"
                type="password"
                name="password"
                placeholder="비밀번호를 입력하세요"
                value={password}
                onChange={handleChange}
              />
            </div>
Kim, Chaerin's avatar
Kim, Chaerin committed
89
          </div>
seoyeon's avatar
seoyeon committed
90
91
92
93
94
95
96
97
          <div className="mt-4 d-flex justify-content-center">
            <button
              type="submit"
              className="btn btn-primary"
              disabled={disabled}
            >
              로그인
            </button>
Kim, Chaerin's avatar
Kim, Chaerin committed
98
99
100
101
          </div>
        </div>
      </form>
    </div>
seoyeon's avatar
0726    
seoyeon committed
102
103
  )
}
Kim, Chaerin's avatar
Kim, Chaerin committed
104

seoyeon's avatar
0726    
seoyeon committed
105
export default Login