Skip to content

Commit bad1128

Browse files
authored
Merge pull request #98 from FE9-2/feat/oauth
Feat: ์นด์นด์˜ค API ํ˜ธ์ถœ์„ ํ†ตํ•ด ์ธ๊ฐ€์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋กœ์ง์ œ๊ฑฐ
2 parents 34c1f7b + 405f554 commit bad1128

File tree

4 files changed

+35
-61
lines changed

4 files changed

+35
-61
lines changed

โ€Žsrc/app/(auth)/signup/applicant/page.tsxโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ export default function ApplicantSignupPage() {
112112
JSON.stringify({ provider: "google", role: "APPLICANT" })
113113
)}`}
114114
>
115-
<Image src="/icons/social/social_google.svg" width={72} height={72} alt="๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ" />
115+
<Image src="/icons/social/social_google.svg" width={72} height={72} alt="๊ตฌ๊ธ€ ํšŒ์›๊ฐ€์ž…" />
116116
</Link>
117117
<Link
118118
href={`https://kauth.kakao.com/oauth/authorize?client_id=${process.env.NEXT_PUBLIC_KAKAO_REST_API_KEY}&redirect_uri=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URI}&response_type=code&state=${encodeURIComponent(
119119
JSON.stringify({ provider: "kakao", role: "APPLICANT" })
120120
)}`}
121121
>
122-
<Image src="/icons/social/social_kakao.svg" width={72} height={72} alt="์นด์นด์˜ค ๋กœ๊ทธ์ธ" />
122+
<Image src="/icons/social/social_kakao.svg" width={72} height={72} alt="์นด์นด์˜ค ํšŒ์›๊ฐ€์ž…" />
123123
</Link>
124124
</div>
125125
</form>

โ€Žsrc/app/(auth)/signup/owner/page.tsxโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ export default function OwnerSignupPage() {
134134
JSON.stringify({ provider: "google", role: "OWNER" })
135135
)}`}
136136
>
137-
<Image src="/icons/social/social_google.svg" width={72} height={72} alt="๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ" />
137+
<Image src="/icons/social/social_google.svg" width={72} height={72} alt="๊ตฌ๊ธ€ ํšŒ์›๊ฐ€์ž…" />
138138
</Link>
139139
<Link
140140
href={`https://kauth.kakao.com/oauth/authorize?client_id=${process.env.NEXT_PUBLIC_KAKAO_REST_API_KEY}&redirect_uri=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URI}&response_type=code&state=${encodeURIComponent(
141141
JSON.stringify({ provider: "kakao", role: "OWNER" })
142142
)}`}
143143
>
144-
<Image src="/icons/social/social_kakao.svg" width={72} height={72} alt="์นด์นด์˜ค ๋กœ๊ทธ์ธ" />
144+
<Image src="/icons/social/social_kakao.svg" width={72} height={72} alt="์นด์นด์˜ค ํšŒ์›๊ฐ€์ž…" />
145145
</Link>
146146
</div>
147147
</form>

โ€Žsrc/app/api/oauth/callback/google/route.tsโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const GET = async (req: NextRequest) => {
4949
},
5050
});
5151

52-
const { access_token, id_token } = tokenResponse.data;
52+
const { id_token } = tokenResponse.data;
5353

5454
// id_token ๋””์ฝ”๋”ฉ
5555
const decodedIdToken = decodeJwt(id_token);
@@ -84,6 +84,6 @@ export const GET = async (req: NextRequest) => {
8484
return response;
8585
} catch (error) {
8686
console.error("Google login error:", error);
87-
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
87+
return NextResponse.json({ message: "์„œ๋ฒ„์—๋Ÿฌ" }, { status: 500 });
8888
}
8989
};
Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import axios from "axios";
32
import { OauthUser } from "@/types/oauth/oauthReq";
43
import apiClient from "@/lib/apiClient";
54

@@ -25,72 +24,47 @@ export const GET = async (req: NextRequest) => {
2524
}
2625
const { provider, role } = parsedState;
2726

28-
const KAKAO_TOKEN_URL = "https://kauth.kakao.com/oauth/token";
29-
const KAKAO_USER_INFO_URL = "https://kapi.kakao.com/v2/user/me";
3027
const clientId = process.env.NEXT_PUBLIC_KAKAO_REST_API_KEY;
3128
const redirectUri = process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URI;
3229

3330
if (!clientId || !redirectUri) {
3431
return NextResponse.json({ message: "Environment variables not set" }, { status: 500 });
3532
}
3633

37-
const params = new URLSearchParams({
38-
grant_type: "authorization_code",
39-
client_id: clientId,
40-
redirect_uri: redirectUri,
41-
code: code,
42-
});
34+
const kakaoUser: OauthUser = {
35+
role: role,
36+
name: "", // ๊ธฐ๋ณธ๊ฐ’ ์„ค์ • (๋นˆ ๋ฌธ์ž์—ด)
37+
token: code, // ์ธ๊ฐ€์ฝ”๋“œ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ
38+
redirectUri: redirectUri,
39+
};
4340

4441
try {
45-
// ์•ก์„ธ์Šค ํ† ํฐ ์š”์ฒญ
46-
const tokenResponse = await axios.post(KAKAO_TOKEN_URL, params);
47-
const { access_token } = tokenResponse.data;
48-
49-
if (!access_token) {
50-
return NextResponse.json({ message: "Failed to retrieve access token" }, { status: 400 });
51-
}
52-
53-
// ์•ก์„ธ์Šค ํ† ํฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ
54-
const userInfoResponse = await axios.get(KAKAO_USER_INFO_URL, {
55-
headers: {
56-
Authorization: `Bearer ${access_token}`,
57-
},
58-
});
59-
60-
const user = userInfoResponse.data;
61-
62-
const kakaoUser: OauthUser = {
63-
role: role,
64-
name: user.properties?.nickname,
65-
token: code, // ์ธ๊ฐ€ ์ฝ”๋“œ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
66-
redirectUri: redirectUri,
67-
};
68-
69-
try {
70-
const kakaoSignupResponse = await apiClient.post(`/oauth/sign-up/${provider}`, kakaoUser);
71-
console.log("์นด์นด์˜ค ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต:", kakaoSignupResponse.data);
72-
} catch (error) {
73-
const errorMessage = (error as any).response?.data;
74-
console.log("์นด์นด์˜ค ํšŒ์›๊ฐ€์ž… ์—๋Ÿฌ", errorMessage);
75-
}
42+
// ์ธ๊ฐ€์ฝ”๋“œ๋ฅผ ํฌํ•จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฑ์—”๋“œ๋กœ ์ „๋‹ฌ
43+
const kakaoSignupResponse = await apiClient.post(`/oauth/sign-up/${provider}`, kakaoUser);
44+
console.log("์นด์นด์˜ค ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต:", kakaoSignupResponse.data);
7645

7746
// ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜
78-
return NextResponse.json(kakaoUser);
79-
} catch (error) {
80-
console.error("Kakao login error:", error);
47+
// return NextResponse.json(kakaoSignupResponse.data);
48+
} catch (error: any) {
49+
// ์—๋Ÿฌ ํƒ€์ž… ๋ช…์‹œ
50+
console.error("์นด์นด์˜ค ํšŒ์›๊ฐ€์ž… ์—๋Ÿฌ:", error.response?.data || error.message);
8151

82-
// Axios ์—๋Ÿฌ์ธ ๊ฒฝ์šฐ ์ƒ์„ธ ์ •๋ณด ์ œ๊ณต
83-
if (axios.isAxiosError(error)) {
84-
const { response } = error;
85-
if (response) {
86-
return NextResponse.json(
87-
{ message: response.data?.msg || "Error during Kakao API call" },
88-
{ status: response.status || 500 }
89-
);
90-
}
91-
}
52+
// return NextResponse.json({ message: error.response?.data || "Error during Kakao signup" }, { status: 500 });
53+
}
9254

93-
// ๊ธฐํƒ€ ์—๋Ÿฌ ์ฒ˜๋ฆฌ
94-
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
55+
try {
56+
// ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜
57+
const response = NextResponse.redirect("http://localhost:3000");
58+
response.cookies.set("user", JSON.stringify(kakaoUser), {
59+
httpOnly: true,
60+
secure: process.env.NODE_ENV === "production",
61+
sameSite: "strict",
62+
maxAge: 60 * 60 * 24, // 1์ผ
63+
path: "/",
64+
});
65+
return response;
66+
} catch (error: any) {
67+
console.error("์นด์นด์˜ค ํšŒ์›๊ฐ€์ž… ์—๋Ÿฌ:", error.response?.data || error.message);
68+
return NextResponse.json({ message: error.response?.data || "์„œ๋ฒ„์—๋Ÿฌ" }, { status: 500 });
9569
}
9670
};

0 commit comments

Comments
ย (0)