Skip to content

Commit c1c675e

Browse files
authored
Merge branch 'dev' into feature/sing-up-page
2 parents 8271877 + f2d90b4 commit c1c675e

2 files changed

Lines changed: 72 additions & 9 deletions

File tree

components/auth/Button.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type ButtonProps = {
77
className?: string;
88
type?: 'button' | 'submit';
99
onClick?: () => void;
10+
disabled?: boolean;
1011
};
1112

1213
const Button = ({
@@ -15,11 +16,13 @@ const Button = ({
1516
className,
1617
type = 'button',
1718
onClick,
19+
disabled,
1820
}: ButtonProps) => {
1921
return (
2022
<button
2123
type={type}
2224
onClick={onClick}
25+
disabled={disabled}
2326
className={clsx(
2427
'h-12 w-full cursor-pointer rounded-lg text-base font-medium transition-all duration-150 active:scale-[0.98]',
2528

components/auth/LoginForm.tsx

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,105 @@ import Link from 'next/link';
77
import { auth } from '@/lib/firebase';
88
import {
99
GithubAuthProvider,
10+
signInWithEmailAndPassword,
1011
signInWithPopup,
1112
UserCredential,
1213
} from 'firebase/auth';
1314
import { useRouter } from 'next/navigation';
15+
import { useState } from 'react';
1416

1517
type LoginFormProps = {
1618
handleOpenModal: () => void;
1719
};
1820

1921
const 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

Comments
 (0)