@@ -7,50 +7,105 @@ import Link from 'next/link';
77import { auth } from '@/lib/firebase' ;
88import {
99 GithubAuthProvider ,
10+ signInWithEmailAndPassword ,
1011 signInWithPopup ,
1112 UserCredential ,
1213} from 'firebase/auth' ;
1314import { useRouter } from 'next/navigation' ;
15+ import { useState } from 'react' ;
1416
1517type LoginFormProps = {
1618 handleOpenModal : ( ) => void ;
1719} ;
1820
1921const LoginForm = ( { handleOpenModal } : LoginFormProps ) => {
22+ const [ email , setEmail ] = useState ( '' ) ;
23+ const [ password , setPassword ] = useState ( '' ) ;
24+ const [ error , setError ] = useState ( '' ) ;
25+ const [ isLoading , setIsLoading ] = useState ( false ) ;
26+
2027 const router = useRouter ( ) ;
2128
2229 const githubProvider = new GithubAuthProvider ( ) ;
30+
2331 const handleGithubLogin = async ( ) => {
2432 try {
2533 const result : UserCredential = await signInWithPopup (
2634 auth ,
2735 githubProvider
2836 ) ;
2937 const user = result . user ;
30- console . log ( '로그인 성공 : ' , user ) ;
38+ // console.log('로그인 성공 : ', user);
3139 router . push ( '/main' ) ;
3240 } catch ( err : unknown ) {
3341 if ( err instanceof Error ) {
34- console . log ( '로그인 에러 : ' , err . message ) ;
3542 }
3643 }
3744 } ;
3845
46+ // 이메일 로그인 메서드
47+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
48+ e . preventDefault ( ) ;
49+ setError ( '' ) ;
50+
51+ if ( isLoading ) return ;
52+ setIsLoading ( true ) ;
53+
54+ // 유효성 검사
55+ if ( ! email || ! password ) {
56+ setError ( '이메일과 비밀번호를 모두 입력해주세요.' ) ;
57+ setIsLoading ( false ) ;
58+ return ;
59+ }
60+
61+ try {
62+ const userCredential = await signInWithEmailAndPassword (
63+ auth ,
64+ email ,
65+ password
66+ ) ;
67+ // console.log('로그인 성공!', userCredential.user);
68+ router . push ( '/main' ) ;
69+ } catch ( err ) {
70+ // console.error('로그인 에러:', err);
71+ setError ( '이메일 또는 비밀번호가 잘못되었습니다.' ) ;
72+ setIsLoading ( false ) ;
73+ }
74+ } ;
75+
3976 return (
40- < >
77+ < form onSubmit = { handleSubmit } >
4178 < div className = "space-y-6" >
42- < FormField id = "email" label = "이메일" type = "email" />
43- < FormField id = "password" label = "비밀번호" type = "password" />
79+ < FormField
80+ id = "email"
81+ label = "이메일"
82+ type = "email"
83+ value = { email }
84+ onChange = { ( e ) => setEmail ( e . target . value ) }
85+ placeholder = "email@example.com"
86+ />
87+ < FormField
88+ id = "password"
89+ label = "비밀번호"
90+ type = "password"
91+ value = { password }
92+ onChange = { ( e ) => setPassword ( e . target . value ) }
93+ placeholder = "비밀번호를 입력하세요"
94+ />
4495 </ div >
4596
97+ { error && < p className = "mt-2 text-sm text-red-500" > { error } </ p > }
98+
4699 < button
47100 type = "button"
48101 className = "text-primary mt-2 ml-auto block cursor-pointer text-xs font-medium"
49102 onClick = { handleOpenModal }
50103 >
51104 비밀번호를 잊으셨나요?
52105 </ button >
53- < Button className = "mt-6" > 이메일로 로그인하기</ Button >
106+ < Button className = "mt-6" type = "submit" >
107+ { isLoading ? '로그인 중...' : '이메일로 로그인하기' }
108+ </ Button >
54109
55110 < p className = "text-text-sub mt-4 text-center text-sm" >
56111 계정이 없으신가요?
@@ -67,11 +122,16 @@ const LoginForm = ({ handleOpenModal }: LoginFormProps) => {
67122 < div className = "bg-border h-px flex-1" />
68123 </ div >
69124
70- < Button variant = "github" >
125+ < Button
126+ variant = "github"
127+ onClick = { handleGithubLogin }
128+ type = "button"
129+ disabled = { isLoading }
130+ >
71131 < FaGithub className = "text-xl" />
72- < span onClick = { handleGithubLogin } > Github로 로그인</ span >
132+ < span > Github로 로그인</ span >
73133 </ Button >
74- </ >
134+ </ form >
75135 ) ;
76136} ;
77137
0 commit comments