Skip to content

Commit 10a08e5

Browse files
authored
Merge pull request #431 from TripInfoWeb/dev/#428
Refactor: 모든 API 요청을 Server Actions로 변경
2 parents 6515eee + 3c55204 commit 10a08e5

File tree

28 files changed

+282
-115
lines changed

28 files changed

+282
-115
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solitour-frontend",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"private": true,
55
"scripts": {
66
"dev": "next dev --turbopack",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
3+
export async function GET(request: NextRequest) {
4+
try {
5+
// Query string 파싱
6+
const url = new URL(request.url);
7+
const code = url.searchParams.get("code");
8+
9+
// 백엔드에 액세스 토큰 요청
10+
const response = await fetch(
11+
`${process.env.BACKEND_URL}/api/auth/oauth2/login?type=kakao&redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${code}`,
12+
{
13+
method: "GET",
14+
headers: { "Content-Type": "application/json" },
15+
cache: "no-store",
16+
},
17+
);
18+
19+
const data = await response.json();
20+
const result = new NextResponse(JSON.stringify(data), {
21+
status: 200,
22+
});
23+
24+
if (response.status == 200) {
25+
const cookies = response.headers.get("set-cookie");
26+
if (cookies) {
27+
// 받은 쿠키를 파싱하여 설정
28+
cookies.split(",").forEach((cookie) => {
29+
result.headers.append("Set-Cookie", cookie.trim());
30+
});
31+
}
32+
}
33+
34+
return result;
35+
} catch (error) {
36+
return new NextResponse("서버 에러", { status: 500 });
37+
}
38+
}

src/entities/diary/api/diaryList.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"use server";
2+
3+
import { cookies } from "next/headers";
14
import { Diary } from "../model/diary";
25
import { fetchWithAuth } from "@/shared/api";
36

@@ -7,11 +10,12 @@ interface DiaryList {
710
}
811

912
export async function getDiaryList(page: number) {
13+
const accessToken = (await cookies()).get("access_token");
1014
const response = await fetchWithAuth(
11-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/diary?page=${page}`,
15+
`${process.env.BACKEND_URL}/api/diary?page=${page}`,
1216
{
1317
method: "GET",
14-
credentials: "include",
18+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
1519
cache: "no-store",
1620
},
1721
);

src/entities/gathering/api/gathering.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
"use server";
2+
13
import { fetchWithAuth } from "@/shared/api";
24
import { GatheringDetail } from "../model/gathering";
5+
import { cookies } from "next/headers";
36

47
export interface GatheringCreateRequest {
58
title: string;
@@ -50,11 +53,12 @@ export interface GatheringUpdateRequest {
5053
}
5154

5255
export async function getGathering(gatheringId: number) {
56+
const accessToken = (await cookies()).get("access_token");
5357
const response = await fetchWithAuth(
54-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings/${gatheringId}`,
58+
`${process.env.BACKEND_URL}/api/gatherings/${gatheringId}`,
5559
{
5660
method: "GET",
57-
credentials: "include",
61+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
5862
cache: "no-store",
5963
},
6064
);
@@ -67,13 +71,16 @@ export async function getGathering(gatheringId: number) {
6771
}
6872

6973
export async function createGathering(data: GatheringCreateRequest) {
74+
const accessToken = (await cookies()).get("access_token");
7075
const response = await fetchWithAuth(
71-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings`,
76+
`${process.env.BACKEND_URL}/api/gatherings`,
7277
{
7378
method: "POST",
74-
headers: { "Content-Type": "application/json" },
79+
headers: {
80+
"Content-Type": "application/json",
81+
Cookie: `${accessToken?.name}=${accessToken?.value}`,
82+
},
7583
body: JSON.stringify(data),
76-
credentials: "include",
7784
cache: "no-store",
7885
},
7986
);
@@ -89,13 +96,16 @@ export async function updateGathering(
8996
gatheringId: number,
9097
data: GatheringUpdateRequest,
9198
) {
99+
const accessToken = (await cookies()).get("access_token");
92100
const response = await fetchWithAuth(
93-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings/${gatheringId}`,
101+
`${process.env.BACKEND_URL}/api/gatherings/${gatheringId}`,
94102
{
95103
method: "PUT",
96-
headers: { "Content-Type": "application/json" },
104+
headers: {
105+
"Content-Type": "application/json",
106+
Cookie: `${accessToken?.name}=${accessToken?.value}`,
107+
},
97108
body: JSON.stringify(data),
98-
credentials: "include",
99109
cache: "no-store",
100110
},
101111
);
@@ -106,11 +116,12 @@ export async function updateGathering(
106116
}
107117

108118
export async function deleteGathering(gatheringId: number) {
119+
const accessToken = (await cookies()).get("access_token");
109120
const response = await fetchWithAuth(
110-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings/${gatheringId}`,
121+
`${process.env.BACKEND_URL}/api/gatherings/${gatheringId}`,
111122
{
112123
method: "DELETE",
113-
credentials: "include",
124+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
114125
cache: "no-store",
115126
},
116127
);

src/entities/gathering/api/gatheringList.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
"use server";
2+
13
import { fetchWithAuth } from "@/shared/api";
24
import { GatheringList } from "../model/gatheringList";
5+
import { cookies } from "next/headers";
36

47
export async function getGatheringList(urlSearch: string) {
8+
const accessToken = (await cookies()).get("access_token");
59
const response = await fetchWithAuth(
6-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings${urlSearch}`,
10+
`${process.env.BACKEND_URL}/api/gatherings${urlSearch}`,
711
{
812
method: "GET",
9-
credentials: "include",
13+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
1014
cache: "no-store",
1115
},
1216
);
@@ -19,11 +23,12 @@ export async function getGatheringList(urlSearch: string) {
1923
}
2024

2125
export async function getGatheringListByTagName(urlSearch: string) {
26+
const accessToken = (await cookies()).get("access_token");
2227
const response = await fetchWithAuth(
23-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings/tag/search${urlSearch}`,
28+
`${process.env.BACKEND_URL}/api/gatherings/tag/search${urlSearch}`,
2429
{
2530
method: "GET",
26-
credentials: "include",
31+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
2732
cache: "no-store",
2833
},
2934
);

src/entities/gathering/api/myPageGatheringList.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
"use server";
2+
13
import { fetchWithAuth } from "@/shared/api";
24
import { GatheringList } from "../model/gatheringList";
5+
import { cookies } from "next/headers";
36

47
export async function getMyPageGatheringList(
58
category: string,
69
currentPage: number,
710
) {
11+
const accessToken = (await cookies()).get("access_token");
812
const response = await fetchWithAuth(
9-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/users/mypage/gathering/${category}?page=${currentPage - 1}`,
13+
`${process.env.BACKEND_URL}/api/users/mypage/gathering/${category}?page=${currentPage - 1}`,
1014
{
1115
method: "GET",
12-
credentials: "include",
16+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
1317
cache: "no-store",
1418
},
1519
);

src/entities/gathering/api/newGatheringList.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
"use server";
2+
13
import { fetchWithAuth } from "@/shared/api";
24
import { Gathering } from "../model/gathering";
5+
import { cookies } from "next/headers";
36

47
export async function getNewGatheringList() {
8+
const accessToken = (await cookies()).get("access_token");
59
const response = await fetchWithAuth(
6-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/gatherings/home`,
10+
`${process.env.BACKEND_URL}/api/gatherings/home`,
711
{
812
method: "GET",
9-
credentials: "include",
13+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
1014
cache: "no-store",
1115
},
1216
);

src/entities/information/api/bestInformationList.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
"use server";
2+
13
import { fetchWithAuth } from "@/shared/api";
4+
import { cookies } from "next/headers";
25

36
interface BestInformationInfo {
47
informationId: number;
@@ -17,11 +20,12 @@ interface BestInformationInfo {
1720
* 좋아요 순으로 3개월 이내에 만들어진 정보 6개를 조회합니다.
1821
*/
1922
export async function getBestInformationList() {
23+
const accessToken = (await cookies()).get("access_token");
2024
const response = await fetchWithAuth(
21-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/informations/main-page`,
25+
`${process.env.BACKEND_URL}/api/informations/main-page`,
2226
{
2327
method: "GET",
24-
credentials: "include",
28+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
2529
cache: "no-store",
2630
},
2731
);

src/entities/information/api/information.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
"use server";
2+
13
import { fetchWithAuth } from "@/shared/api";
24
import { Information } from "../model/information";
5+
import { cookies } from "next/headers";
36

47
export interface InformationDetailResponse {
58
title: string;
@@ -84,11 +87,12 @@ export interface InformationUpdateRequest {
8487
}
8588

8689
export async function getInformation(informationId: number) {
90+
const accessToken = (await cookies()).get("access_token");
8791
const response = await fetchWithAuth(
88-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/informations/${informationId}`,
92+
`${process.env.BACKEND_URL}/api/informations/${informationId}`,
8993
{
9094
method: "GET",
91-
credentials: "include",
95+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
9296
cache: "no-store",
9397
},
9498
);
@@ -101,13 +105,16 @@ export async function getInformation(informationId: number) {
101105
}
102106

103107
export async function createInformation(data: InformationCreateRequest) {
108+
const accessToken = (await cookies()).get("access_token");
104109
const response = await fetchWithAuth(
105-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/informations`,
110+
`${process.env.BACKEND_URL}/api/informations`,
106111
{
107112
method: "POST",
108-
headers: { "Content-Type": "application/json" },
113+
headers: {
114+
"Content-Type": "application/json",
115+
Cookie: `${accessToken?.name}=${accessToken?.value}`,
116+
},
109117
body: JSON.stringify(data),
110-
credentials: "include",
111118
cache: "no-store",
112119
},
113120
);
@@ -123,13 +130,16 @@ export async function updateInformation(
123130
informationId: number,
124131
data: InformationUpdateRequest,
125132
) {
133+
const accessToken = (await cookies()).get("access_token");
126134
const response = await fetchWithAuth(
127-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/informations/${informationId}`,
135+
`${process.env.BACKEND_URL}/api/informations/${informationId}`,
128136
{
129137
method: "PUT",
130-
headers: { "Content-Type": "application/json" },
138+
headers: {
139+
"Content-Type": "application/json",
140+
Cookie: `${accessToken?.name}=${accessToken?.value}`,
141+
},
131142
body: JSON.stringify(data),
132-
credentials: "include",
133143
cache: "no-store",
134144
},
135145
);
@@ -140,11 +150,12 @@ export async function updateInformation(
140150
}
141151

142152
export async function deleteInformation(informationId: number) {
153+
const accessToken = (await cookies()).get("access_token");
143154
const response = await fetchWithAuth(
144-
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/informations/${informationId}`,
155+
`${process.env.BACKEND_URL}/api/informations/${informationId}`,
145156
{
146157
method: "DELETE",
147-
credentials: "include",
158+
headers: { Cookie: `${accessToken?.name}=${accessToken?.value}` },
148159
cache: "no-store",
149160
},
150161
);

0 commit comments

Comments
 (0)