|
1 | 1 | // 로그인 페이지 |
2 | 2 | 'use client'; |
3 | | -import Form from '@/components/Form'; |
4 | | -import { http } from '@/lib/api/client'; |
5 | | -import type { LoginRequest, LoginResponse } from '@/lib/types'; |
| 3 | +import { useState, FormEvent } from 'react'; |
| 4 | +import { authApi } from '@/lib/api/auth'; |
| 5 | +import type { LoginRequest } from '@/types/auth'; |
6 | 6 | import { useToast } from '@/components/ui/Toast'; |
7 | 7 | import { useAuth } from '@/hooks/auth/useAuth'; |
8 | 8 | import { useRouter } from 'next/navigation'; |
| 9 | +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; |
| 10 | +import { Label } from '@/components/ui/label'; |
| 11 | +import { Input } from '@/components/ui/input'; |
| 12 | +import { Button } from '@/components/ui/Button'; |
9 | 13 |
|
10 | 14 | export default function LoginPage() { |
11 | 15 | const toast = useToast(); |
12 | 16 | const auth = useAuth(); |
13 | 17 | const router = useRouter(); |
| 18 | + const [username, setUsername] = useState(''); |
| 19 | + const [password, setPassword] = useState(''); |
| 20 | + const [loading, setLoading] = useState(false); |
14 | 21 |
|
15 | | - async function submit(values: Record<string,string>) { |
16 | | - const payload: LoginRequest = { username: values.username, password: values.password }; |
| 22 | + async function submit(e: FormEvent) { |
| 23 | + e.preventDefault(); |
| 24 | + const payload: LoginRequest = { username, password }; |
17 | 25 | try { |
18 | | - const res = await http.post<LoginResponse>('/api/v1/auth/login', payload, 'cookie'); // 쿠키 세션 예시 |
| 26 | + setLoading(true); |
| 27 | + const res = await authApi.login(payload); |
19 | 28 | if (res.accessToken) { |
20 | 29 | auth.loginWithToken(res.accessToken); |
21 | 30 | } |
22 | 31 | toast.push(res.message ?? '로그인 성공'); |
23 | 32 | router.push('/'); |
24 | 33 | } catch (e: any) { |
25 | 34 | toast.push(`로그인 실패: ${e.message}`); |
| 35 | + } finally { |
| 36 | + setLoading(false); |
26 | 37 | } |
27 | 38 | } |
28 | 39 |
|
29 | 40 | return ( |
30 | | - <section> |
31 | | - <h1>로그인</h1> |
32 | | - <Form |
33 | | - fields={[ |
34 | | - { name: 'username', label: '아이디' }, |
35 | | - { name: 'password', label: '비밀번호', type: 'password' }, |
36 | | - ]} |
37 | | - onSubmit={submit} |
38 | | - submitText="로그인" |
39 | | - /> |
40 | | - <p style={{marginTop:12, opacity:.7}}>스프링 엔드포인트: POST /api/v1/auth/login</p> |
41 | | - </section> |
| 41 | + <div className="min-h-screen bg-background"> |
| 42 | + <div className="container mx-auto px-4 sm:px-6 lg:px-8 py-16"> |
| 43 | + <div className="mx-auto max-w-md"> |
| 44 | + <Card className="shadow-lg"> |
| 45 | + <CardHeader> |
| 46 | + <CardTitle className="text-2xl">로그인</CardTitle> |
| 47 | + </CardHeader> |
| 48 | + <CardContent> |
| 49 | + <form className="space-y-6" onSubmit={submit}> |
| 50 | + <div className="space-y-2"> |
| 51 | + <Label htmlFor="username">아이디</Label> |
| 52 | + <Input id="username" value={username} onChange={(e) => setUsername(e.target.value)} required /> |
| 53 | + </div> |
| 54 | + <div className="space-y-2"> |
| 55 | + <Label htmlFor="password">비밀번호</Label> |
| 56 | + <Input id="password" type="password" value={password} onChange={(e) => setPassword(e.target.value)} required /> |
| 57 | + </div> |
| 58 | + <Button type="submit" className="w-full" size="lg" disabled={loading}> |
| 59 | + {loading ? '처리중...' : '로그인'} |
| 60 | + </Button> |
| 61 | + <p className="text-xs text-muted-foreground">스프링 엔드포인트: POST /api/v1/auth/login</p> |
| 62 | + </form> |
| 63 | + </CardContent> |
| 64 | + </Card> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </div> |
42 | 68 | ); |
43 | 69 | } |
0 commit comments