@@ -7,50 +7,106 @@ 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' ) ;
32- } catch ( error : unknown ) {
33- if ( error instanceof Error ) {
34- console . log ( '로그인 에러 : ' , error . message ) ;
40+ } catch ( err : unknown ) {
41+ if ( err instanceof Error ) {
42+ // console.log('로그인 에러 : ', err .message);
3543 }
3644 }
3745 } ;
3846
47+ // 이메일 로그인 메서드
48+ const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
49+ e . preventDefault ( ) ;
50+ setError ( '' ) ;
51+
52+ if ( isLoading ) return ;
53+ setIsLoading ( true ) ;
54+
55+ // 유효성 검사
56+ if ( ! email || ! password ) {
57+ setError ( '이메일과 비밀번호를 모두 입력해주세요.' ) ;
58+ setIsLoading ( false ) ;
59+ return ;
60+ }
61+
62+ try {
63+ const userCredential = await signInWithEmailAndPassword (
64+ auth ,
65+ email ,
66+ password
67+ ) ;
68+ // console.log('로그인 성공!', userCredential.user);
69+ router . push ( '/main' ) ;
70+ } catch ( err ) {
71+ // console.error('로그인 에러:', err);
72+ setError ( '이메일 또는 비밀번호가 잘못되었습니다.' ) ;
73+ setIsLoading ( false ) ;
74+ }
75+ } ;
76+
3977 return (
40- < >
78+ < form onSubmit = { handleSubmit } >
4179 < div className = "space-y-6" >
42- < FormField id = "email" label = "이메일" type = "email" />
43- < FormField id = "password" label = "비밀번호" type = "password" />
80+ < FormField
81+ id = "email"
82+ label = "이메일"
83+ type = "email"
84+ value = { email }
85+ onChange = { ( e ) => setEmail ( e . target . value ) }
86+ placeholder = "email@example.com"
87+ />
88+ < FormField
89+ id = "password"
90+ label = "비밀번호"
91+ type = "password"
92+ value = { password }
93+ onChange = { ( e ) => setPassword ( e . target . value ) }
94+ placeholder = "비밀번호를 입력하세요"
95+ />
4496 </ div >
4597
98+ { error && < p className = "mt-2 text-sm text-red-500" > { error } </ p > }
99+
46100 < button
47101 type = "button"
48102 className = "text-primary mt-2 ml-auto block cursor-pointer text-xs font-medium"
49103 onClick = { handleOpenModal }
50104 >
51105 비밀번호를 잊으셨나요?
52106 </ button >
53- < Button className = "mt-6" > 이메일로 로그인하기</ Button >
107+ < Button className = "mt-6" type = "submit" >
108+ { isLoading ? '로그인 중...' : '이메일로 로그인하기' }
109+ </ Button >
54110
55111 < p className = "text-text-sub mt-4 text-center text-sm" >
56112 계정이 없으신가요?
@@ -67,11 +123,16 @@ const LoginForm = ({ handleOpenModal }: LoginFormProps) => {
67123 < div className = "bg-border h-px flex-1" />
68124 </ div >
69125
70- < Button variant = "github" >
126+ < Button
127+ variant = "github"
128+ onClick = { handleGithubLogin }
129+ type = "button"
130+ disabled = { isLoading }
131+ >
71132 < FaGithub className = "text-xl" />
72- < span onClick = { handleGithubLogin } > Github로 로그인</ span >
133+ < span > Github로 로그인</ span >
73134 </ Button >
74- </ >
135+ </ form >
75136 ) ;
76137} ;
77138
0 commit comments