|
1 | 1 | import { Input } from '@/shared/ui/Input'; |
2 | 2 | import { Button } from '@/shared/ui/button'; |
3 | 3 | import { useState } from 'react'; |
4 | | -import { useAuth } from '../model/useAuth'; |
| 4 | +import { useLogin } from '../hooks/useLogin'; |
5 | 5 |
|
6 | 6 | export function LoginForm() { |
7 | | - const { login } = useAuth(); |
8 | | - const [username, setUsername] = useState(''); |
| 7 | + const { mutate, isPending, isError, error } = useLogin(); |
| 8 | + const [email, setEmail] = useState(''); |
9 | 9 | const [password, setPassword] = useState(''); |
10 | | - const [error, setError] = useState<string | null>(null); |
11 | | - const [loading, setLoading] = useState(false); |
12 | 10 |
|
13 | | - const handleSubmit = async (e: React.FormEvent) => { |
| 11 | + const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { |
14 | 12 | e.preventDefault(); |
15 | | - setError(null); |
16 | | - setLoading(true); |
17 | | - try { |
18 | | - await login(username, password); |
19 | | - // 성공시 라우팅 처리 필요하면 여기서! |
20 | | - } catch (err) { |
21 | | - setError('아이디 또는 비밀번호가 올바르지 않습니다.'); |
22 | | - } finally { |
23 | | - setLoading(false); |
24 | | - } |
| 13 | + mutate({ email, password }); |
25 | 14 | }; |
26 | 15 |
|
27 | 16 | return ( |
28 | 17 | <form className='flex flex-col gap-4 w-full' onSubmit={handleSubmit}> |
29 | 18 | <Input |
30 | | - label='아이디' |
31 | | - placeholder='아이디' |
32 | | - value={username} |
33 | | - onChange={(e) => setUsername(e.target.value)} |
34 | | - minLength={6} |
35 | | - maxLength={16} |
| 19 | + label='이메일' |
| 20 | + placeholder='이메일' |
| 21 | + value={email} |
| 22 | + onChange={(e) => setEmail(e.target.value)} |
36 | 23 | required |
37 | | - autoComplete='username' |
| 24 | + autoComplete='email' |
38 | 25 | /> |
39 | 26 | <Input |
40 | 27 | label='비밀번호' |
41 | 28 | placeholder='비밀번호' |
42 | 29 | value={password} |
43 | 30 | onChange={(e) => setPassword(e.target.value)} |
44 | | - minLength={6} |
45 | | - maxLength={20} |
| 31 | + type='password' |
46 | 32 | required |
47 | 33 | autoComplete='current-password' |
48 | 34 | /> |
49 | | - {error && ( |
| 35 | + {isError && error instanceof Error && ( |
50 | 36 | <div className='text-[var(--danger-text-default)] font-display-medium-16 px-2'> |
51 | | - {error} |
| 37 | + {error.message} |
52 | 38 | </div> |
53 | 39 | )} |
54 | 40 | <Button |
55 | 41 | variant='contained' |
56 | 42 | type='submit' |
57 | 43 | size='lg' |
58 | | - disabled={loading || !username || !password} |
| 44 | + disabled={isPending || !email || !password} |
59 | 45 | className='w-full' |
60 | 46 | > |
61 | 47 | <span className='font-available-medium-20'> |
62 | | - {loading ? '로그인 중...' : '아이디로 로그인'} |
| 48 | + {isPending ? '로그인 중...' : '이메일로 로그인'} |
63 | 49 | </span> |
64 | 50 | </Button> |
65 | 51 | </form> |
|
0 commit comments