import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { useToast } from "@/hooks/use-toast"; import { queryClient } from "@/lib/queryClient"; interface LoginModalProps { isOpen: boolean; onClose: () => void; } export default function LoginModal({ isOpen, onClose }: LoginModalProps) { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const { toast } = useToast(); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { const response = await fetch("/api/login", { method: "POST", headers: { "Content-Type": "application/json", }, credentials: "include", body: JSON.stringify({ username, password }), }); if (response.ok) { const user = await response.json(); toast({ title: "로그인 성공", description: `환영합니다, ${user.firstName}님!`, }); // Invalidate auth queries to refresh user state queryClient.invalidateQueries({ queryKey: ["/api/auth/user"] }); // Reset form and close modal setUsername(""); setPassword(""); onClose(); } else { const error = await response.json(); toast({ title: "로그인 실패", description: error.message || "잘못된 아이디 또는 비밀번호입니다.", variant: "destructive", }); } } catch (error) { toast({ title: "로그인 오류", description: "로그인 중 오류가 발생했습니다.", variant: "destructive", }); } finally { setIsLoading(false); } }; return ( 로그인
setUsername(e.target.value)} onInvalid={(e) => (e.target as HTMLInputElement).setCustomValidity('Please enter your username')} onInput={(e) => (e.target as HTMLInputElement).setCustomValidity('')} placeholder="아이디를 입력하세요" required data-testid="input-username" />
setPassword(e.target.value)} onInvalid={(e) => (e.target as HTMLInputElement).setCustomValidity('Please enter your password')} onInput={(e) => (e.target as HTMLInputElement).setCustomValidity('')} placeholder="비밀번호를 입력하세요" required data-testid="input-password" />
); }