Skip to content

Commit 0696dc7

Browse files
authored
Merge pull request #35 from DeveloperBlog-Devflow/feature/login-page
feat: github 회원가입 시 유저 정보 DB에 저장
2 parents a202fb6 + 3db1a03 commit 0696dc7

2 files changed

Lines changed: 46 additions & 22 deletions

File tree

api/signup.api.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { auth, db } from '@/lib/firebase';
2-
import { createUserWithEmailAndPassword, updateProfile } from 'firebase/auth';
3-
import { doc, serverTimestamp, setDoc } from 'firebase/firestore';
2+
import {
3+
createUserWithEmailAndPassword,
4+
updateProfile,
5+
GithubAuthProvider,
6+
signInWithPopup,
7+
getAdditionalUserInfo,
8+
} from 'firebase/auth';
9+
import { getDoc, doc, serverTimestamp, setDoc } from 'firebase/firestore';
410

511
export type SignupPayload = {
612
nickname: string;
713
email: string;
814
password: string;
9-
streakDays: number;
10-
tilCount: number;
1115
};
1216

13-
export async function signupWithEmail(p: SignupPayload) {
17+
export const signupWithEmail = async (p: SignupPayload) => {
1418
const cred = await createUserWithEmailAndPassword(auth, p.email, p.password);
1519
await updateProfile(cred.user, { displayName: p.nickname });
1620
await setDoc(doc(db, 'users', cred.user.uid), {
@@ -21,4 +25,31 @@ export async function signupWithEmail(p: SignupPayload) {
2125
tilCount: 0,
2226
});
2327
return cred.user;
24-
}
28+
};
29+
30+
export const signupWithGithub = async () => {
31+
const provider = new GithubAuthProvider();
32+
33+
const result = await signInWithPopup(auth, provider);
34+
const user = result.user;
35+
36+
const info = getAdditionalUserInfo(result);
37+
const githubUsername = info?.username ?? null;
38+
39+
const userRef = doc(db, 'users', user.uid);
40+
const snap = await getDoc(userRef);
41+
42+
if (!snap.exists()) {
43+
await setDoc(userRef, {
44+
userId: user.uid,
45+
nickname: user.displayName ?? githubUsername ?? '익명',
46+
email: user.email ?? '',
47+
provider: 'github.com',
48+
githubUsername,
49+
createdAt: serverTimestamp(),
50+
streakDays: 0,
51+
tilCount: 0,
52+
});
53+
}
54+
return user;
55+
};

components/auth/LoginForm.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import { FaGithub } from 'react-icons/fa';
55
import Button from '@/components/auth/Button';
66
import Link from 'next/link';
77
import { auth } from '@/lib/firebase';
8-
import {
9-
GithubAuthProvider,
10-
signInWithEmailAndPassword,
11-
signInWithPopup,
12-
UserCredential,
13-
} from 'firebase/auth';
8+
import { signInWithEmailAndPassword } from 'firebase/auth';
149
import { useRouter } from 'next/navigation';
1510
import { useState } from 'react';
11+
import { signupWithGithub } from '@/api/signup.api';
1612

1713
type LoginFormProps = {
1814
handleOpenModal: () => void;
@@ -26,20 +22,17 @@ const LoginForm = ({ handleOpenModal }: LoginFormProps) => {
2622

2723
const router = useRouter();
2824

29-
const githubProvider = new GithubAuthProvider();
30-
3125
const handleGithubLogin = async () => {
26+
if (isLoading) return;
27+
setIsLoading(true);
28+
setError('');
3229
try {
33-
const result: UserCredential = await signInWithPopup(
34-
auth,
35-
githubProvider
36-
);
37-
const user = result.user;
38-
// console.log('로그인 성공 : ', user);
30+
await signupWithGithub();
3931
router.push('/');
4032
} catch (err: unknown) {
41-
if (err instanceof Error) {
42-
}
33+
setError('Github 로그인에 실패했습니다.');
34+
} finally {
35+
setIsLoading(false);
4336
}
4437
};
4538

0 commit comments

Comments
 (0)