|
1 | 1 | import { useState } from 'react'; |
2 | | -import { useDispatch } from 'react-redux'; |
3 | | -import { clearAuth, setAuth } from '@/store/slices/authSlice'; |
4 | | -import { login } from '@/apis/user/login'; |
5 | | -import { useSelector } from 'react-redux'; |
6 | | -import { RootState } from '@/store'; |
| 2 | +import { useAuth } from '@/hook/useAuth'; |
7 | 3 |
|
8 | 4 | const Login = () => { |
9 | | - const accessToken = useSelector((state: RootState) => state.auth.accessToken); |
10 | | - |
11 | | - const dispatch = useDispatch(); |
| 5 | + const { login, logout } = useAuth(); |
12 | 6 | const [username, setUsername] = useState(''); |
13 | 7 | const [password, setPassword] = useState(''); |
14 | 8 |
|
15 | 9 | const handleLogin = async () => { |
16 | 10 | try { |
17 | | - const response = await login({ username, password }); |
18 | | - const { user, accessToken, refreshToken } = response; |
19 | | - |
20 | | - dispatch( |
21 | | - setAuth({ |
22 | | - user, |
23 | | - accessToken, |
24 | | - refreshToken, |
25 | | - }), |
26 | | - ); |
27 | | - |
28 | | - setUsername(''); |
29 | | - setPassword(''); |
30 | | - |
31 | | - alert('로그인 성공!'); |
32 | | - } catch (error) { |
| 11 | + await login(username, password); |
| 12 | + } catch (e) { |
33 | 13 | alert('로그인 실패'); |
34 | | - console.error(error); |
| 14 | + console.error(e); |
35 | 15 | } |
36 | 16 | }; |
37 | 17 |
|
38 | | - const handleLogout = () => { |
39 | | - dispatch(clearAuth()); |
40 | | - localStorage.removeItem('accessToken'); |
41 | | - }; |
42 | | - |
43 | | - if (!accessToken) { |
44 | | - return ( |
45 | | - <div className="flex flex-col items-center justify-center h-screen gap-4"> |
46 | | - <h2 className="text-2xl font-bold">로그인 테스트 페이지</h2> |
47 | | - <input |
48 | | - type="text" |
49 | | - placeholder="아이디" |
50 | | - value={username} |
51 | | - onChange={(e) => setUsername(e.target.value)} |
52 | | - className="p-2 border rounded" |
53 | | - /> |
54 | | - <input |
55 | | - type="password" |
56 | | - placeholder="비밀번호" |
57 | | - value={password} |
58 | | - onChange={(e) => setPassword(e.target.value)} |
59 | | - className="p-2 border rounded" |
60 | | - /> |
| 18 | + return ( |
| 19 | + <div className="flex items-center justify-center min-h-screen bg-gray-100"> |
| 20 | + <div className="bg-white shadow-xl rounded-2xl p-8 w-full max-w-sm"> |
| 21 | + <h2 className="font-gumi text-2xl font-bold text-center mb-6"> |
| 22 | + <span className="text-(--color-primary-base)">너</span>로 정했다! |
| 23 | + </h2> |
| 24 | + <div className="mb-4"> |
| 25 | + <label className="font-pm block text-gray-700 mb-2">아이디</label> |
| 26 | + <input |
| 27 | + type="text" |
| 28 | + value={username} |
| 29 | + onChange={(e) => setUsername(e.target.value)} |
| 30 | + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-(--color-primary-base)/70" |
| 31 | + placeholder="아이디를 입력하세요" |
| 32 | + /> |
| 33 | + </div> |
| 34 | + <div className="mb-6"> |
| 35 | + <label className="font-pm block text-gray-700 mb-2">비밀번호</label> |
| 36 | + <input |
| 37 | + type="password" |
| 38 | + value={password} |
| 39 | + onChange={(e) => setPassword(e.target.value)} |
| 40 | + className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-(--color-primary-base)/70" |
| 41 | + placeholder="비밀번호를 입력하세요" |
| 42 | + /> |
| 43 | + </div> |
61 | 44 | <button |
62 | 45 | onClick={handleLogin} |
63 | | - className="px-4 py-2 text-white bg-blue-500 rounded hover:bg-blue-600" |
| 46 | + className="w-full py-2 bg-(--color-primary-base) text-white font-semibold rounded-lg hover:bg-(--color-primary-hover) transition duration-200" |
64 | 47 | > |
65 | 48 | 로그인 |
66 | 49 | </button> |
67 | | - </div> |
68 | | - ); |
69 | | - } else { |
70 | | - return ( |
71 | | - <div> |
72 | | - <div>로그인 중 입니다.</div> |
73 | | - <button className="bg-amber-300" onClick={handleLogout}> |
| 50 | + <button |
| 51 | + onClick={logout} |
| 52 | + className="mt-4 w-full py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition duration-200" |
| 53 | + > |
74 | 54 | 로그아웃 |
75 | 55 | </button> |
76 | 56 | </div> |
77 | | - ); |
78 | | - } |
| 57 | + </div> |
| 58 | + ); |
79 | 59 | }; |
80 | 60 |
|
81 | 61 | export default Login; |
0 commit comments