Skip to content

Commit 7617db7

Browse files
committed
feat(social-auth) : 카카오, 구글 소셜 로그인 구현
1 parent 62bc3d9 commit 7617db7

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
import { useRouter, useSearchParams } from "next/navigation";
5+
6+
export default function KakaoCallbackPage() {
7+
const router = useRouter();
8+
const params = useSearchParams();
9+
const code = params.get("code");
10+
11+
useEffect(() => {
12+
if (!code) return;
13+
14+
const login = async () => {
15+
const res = await fetch(
16+
`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/v1/auth/login/google?code=${code}`,
17+
{
18+
method: "GET",
19+
credentials: "include", // 쿠키 방식이면 필수
20+
}
21+
);
22+
23+
if (res.ok) {
24+
router.replace("/home");
25+
} else {
26+
router.replace("/sign-in");
27+
}
28+
};
29+
30+
login();
31+
}, [code, router]);
32+
33+
return <p>구글 로그인 처리 중...</p>;
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
import { useRouter, useSearchParams } from "next/navigation";
5+
6+
export default function KakaoCallbackPage() {
7+
const router = useRouter();
8+
const params = useSearchParams();
9+
const code = params.get("code");
10+
11+
useEffect(() => {
12+
if (!code) return;
13+
14+
const login = async () => {
15+
const res = await fetch(
16+
`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/v1/auth/login/kakao?code=${code}`,
17+
{
18+
method: "GET",
19+
credentials: "include", // 쿠키 방식이면 필수
20+
}
21+
);
22+
23+
if (res.ok) {
24+
router.replace("/home");
25+
} else {
26+
router.replace("/sign-in");
27+
}
28+
};
29+
30+
login();
31+
}, [code, router]);
32+
33+
return <p>카카오 로그인 처리 중...</p>;
34+
}

src/components/auth/SocialButton.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
"use client";
2+
13
import { MessageCircle } from "lucide-react";
24
import { Button } from "@/components/ui/button";
35
import Image from "next/image";
46

7+
const KAKAO_AUTH_URL =
8+
"https://kauth.kakao.com/oauth/authorize" +
9+
`?response_type=code` +
10+
`&client_id=${process.env.NEXT_PUBLIC_KAKAO_CLIENT_ID}` +
11+
`&redirect_uri=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URI}`;
12+
13+
const GOOGLE_AUTH_URL =
14+
"https://accounts.google.com/o/oauth2/v2/auth" +
15+
`?response_type=code` +
16+
`&client_id=${process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID}` +
17+
`&redirect_uri=${process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI}` +
18+
`&scope=openid%20email%20profile`;
19+
520
export default function SocialButton() {
21+
const handleKakaoLogin = () => {
22+
window.location.href = KAKAO_AUTH_URL;
23+
};
24+
25+
const handleGoogleLogin = () => {
26+
window.location.href = GOOGLE_AUTH_URL;
27+
};
28+
629
return (
730
<>
831
<div className="social bg-bg-main flex w-full flex-col gap-3">
@@ -11,6 +34,7 @@ export default function SocialButton() {
1134
variant="default"
1235
size="lg"
1336
asChild={false}
37+
onClick={handleKakaoLogin}
1438
>
1539
<MessageCircle />
1640
Kakao로 계속하기
@@ -21,6 +45,7 @@ export default function SocialButton() {
2145
variant="outline"
2246
size="lg"
2347
asChild={false}
48+
onClick={handleGoogleLogin}
2449
>
2550
<Image src="/googleIcon.svg" alt="Google icon" width={15} height={15} />
2651
Google로 계속하기

0 commit comments

Comments
 (0)