Skip to content

Commit

Permalink
Merge pull request #401 from TripInfoWeb/dev
Browse files Browse the repository at this point in the history
Release: v1.0.3
  • Loading branch information
ssssksss authored Oct 1, 2024
2 parents a260cc0 + ba548e9 commit 87b0581
Show file tree
Hide file tree
Showing 56 changed files with 1,246 additions and 439 deletions.
9 changes: 9 additions & 0 deletions public/user/naver-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 15 additions & 17 deletions src/app/api/auth/kakao/getToken/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function GET(request: NextRequest) {
const code = url.searchParams.get("code");

// 백엔드에 액세스 토큰 재요청
const backendResponse = await fetch(
const response = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login?type=kakao&redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${code}`,
{
method: "GET",
Expand All @@ -18,17 +18,15 @@ export async function GET(request: NextRequest) {
credentials: "include",
},
);


const result = new NextResponse(
backendResponse.status == 200 ? "성공" : "실패",
{
status: backendResponse.status,
headers: { "Content-Type": "application/json" },
},
);

if (backendResponse.status == 200) {
const cookies = backendResponse.headers.get("set-cookie");
const data = await response.json();
const result = new NextResponse(JSON.stringify(data), {
status: 200,
});

if (response.status == 200) {
const cookies = response.headers.get("set-cookie");
if (cookies) {
// 받은 쿠키를 파싱하여 설정
cookies.split(",").forEach((cookie) => {
Expand All @@ -51,8 +49,8 @@ export async function POST(request: NextRequest) {
const body = await request.json();

// 백엔드에 액세스 토큰 재요청
const backendResponse = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login/kakao?redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${code}`,
const response = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login?redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${code}`,
{
method: "POST",
headers: {
Expand All @@ -69,15 +67,15 @@ export async function POST(request: NextRequest) {
);

const result = new NextResponse(
backendResponse.status == 200 ? "성공" : "실패",
response.status == 200 ? "성공" : "실패",
{
status: backendResponse.status,
status: response.status,
headers: { "Content-Type": "application/json" },
},
);

if (backendResponse.status == 200) {
const cookies = backendResponse.headers.get("set-cookie");
if (response.status == 200) {
const cookies = response.headers.get("set-cookie");
if (cookies) {
// 받은 쿠키를 파싱하여 설정
cookies.split(",").forEach((cookie) => {
Expand Down
90 changes: 90 additions & 0 deletions src/app/api/auth/naver/getToken/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
try {
// Query string 파싱
const url = new URL(request.url);
const code = url.searchParams.get("code");

// 백엔드에 액세스 토큰 재요청
const response = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login?type=naver&redirectUrl=${process.env.NEXT_PUBLIC_NAVER_REDIRECT_URL}&code=${code}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
cache: "no-store",
credentials: "include",
},
);

const data = await response.json();
const result = new NextResponse(JSON.stringify(data), {
status: 200,
});

if (response.ok) {
const cookies = response.headers.get("set-cookie");
if (cookies) {
// 받은 쿠키를 파싱하여 설정
cookies.split(",").forEach((cookie) => {
result.headers.append("Set-Cookie", cookie.trim());
});
}
}

return result;
} catch (error) {
return new NextResponse("서버 에러", { status: 500 });
}
}

export async function POST(request: NextRequest) {
try {
// Query string 파싱
const url = new URL(request.url);
const code = url.searchParams.get("code");
const body = await request.json();

// 백엔드에 액세스 토큰 재요청
const response = await fetch(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/oauth2/login/kakao?redirectUrl=${process.env.NEXT_PUBLIC_KAKAO_REDIRECT_URL}&code=${code}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
createUserInfoRequest: {
...body
}
}),
cache: "no-store",
credentials: "include",
},
);

const result = new NextResponse(
response.status == 200 ? "성공" : "실패",
{
status: response.status,
headers: { "Content-Type": "application/json" },
},
);

if (response.status == 200) {
const cookies = response.headers.get("set-cookie");
if (cookies) {
// 받은 쿠키를 파싱하여 설정
cookies.split(",").forEach((cookie) => {
result.headers.append("Set-Cookie", cookie.trim());
});
}
}

return result;
} catch (error) {
return new NextResponse("서버 에러", { status: 500 });
}
}
29 changes: 29 additions & 0 deletions src/app/api/auth/naver/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { NextResponse } from "next/server";

function generateRandomString(length: number): string {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters[randomIndex];
}
return result;
}

export function GET() {
try {
const clientId = process.env.NAVER_CLIENT_ID;
const redirectUri = process.env.NEXT_PUBLIC_NAVER_REDIRECT_URL;
const RANDOM_STATE = generateRandomString(150);
const timestamp = new Date().toISOString(); // Get the current timestamp in ISO format

if (!clientId || !redirectUri) {
return NextResponse.redirect("/auth/signin");
}

const naverAuthUrl = `https://nid.naver.com/oauth2.0/authorize?client_id=${clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&state=${RANDOM_STATE}`;
return NextResponse.redirect(naverAuthUrl);
} catch (error) {
return NextResponse.redirect("/auth/signin");
}
}
118 changes: 118 additions & 0 deletions src/app/api/auth/user/info/agree/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
// const access_cookie = request.cookies.get("access_token");
// if (!access_cookie) {
// const refresh_cookie = request.cookies.get("refresh_token");
// if (!refresh_cookie) {
// // 리프레시 토큰이 없으므로 요청 중단
// return new NextResponse("Refresh token not found", { status: 403 });
// }
// // 리프레시 토큰으로 재발급 받아 재요청 보내기 위한 응답
// return new NextResponse("Refresh token not found", { status: 401 });
// }

// // 사용자 정보 조회 API
// const response = await fetch(`${process.env.BACKEND_URL}/api/users/info`, {
// method: "GET",
// headers: {
// Cookie: `${access_cookie?.name}=${access_cookie?.value}`,
// "Content-Type": "application/json",
// "Access-Control-Allow-Origin": "*",
// },
// cache: "no-store",
// });

// if (response.ok) {
// const data = await response.json();
// return new NextResponse(JSON.stringify(data), {
// status: 200,
// });
// }

// if (response.status == 401) {
// return new NextResponse("토큰 만료", {
// status: 401,
// });
// }

// cookies().delete("access_token");
// cookies().delete("refresh_token");
// return new NextResponse("서버 에러", {
// status: 500,
// });
}

export async function PUT(request: NextRequest) {
const access_cookie = request.cookies.get("access_token");
if (!access_cookie) {
const refresh_cookie = request.cookies.get("refresh_token");
if (!refresh_cookie) {
// 리프레시 토큰이 없으므로 요청 중단
return new NextResponse("Refresh token not found", { status: 403 });
}
// 리프레시 토큰으로 재발급 받아 재요청 보내기 위한 응답
return new NextResponse("Refresh token not found", { status: 401 });
}

const requestData = await request.json();

// 사용자 정보 조회 API
const response = await fetch(`${process.env.BACKEND_URL}/api/users/info/agree`, {
method: "PUT",
headers: {
Cookie: `${access_cookie?.name}=${access_cookie?.value}`,
"Content-Type": "application/json",
},
body: JSON.stringify(requestData),
cache: "no-store",
});

if (response.ok) {
return response;
}

return new NextResponse("서버 에러", {
status: 500,
});
}

export async function DELETE(request: NextRequest) {
// const access_cookie = request.cookies.get("access_token");
// if (!access_cookie) {
// const refresh_cookie = request.cookies.get("refresh_token");
// if (!refresh_cookie) {
// // 리프레시 토큰이 없으므로 요청 중단
// return new NextResponse("Refresh token not found", { status: 403 });
// }
// // 리프레시 토큰으로 재발급 받아 재요청 보내기 위한 응답
// return new NextResponse("Refresh token not found", { status: 401 });
// }

// const url = new URL(request.url);

// // 사용자 삭제
// const response = await fetchWithAuth(
// `${process.env.BACKEND_URL}/api/auth/oauth2?type=${url.searchParams.get("type")}`,
// {
// method: "DELETE",
// headers: {
// Cookie: `${access_cookie?.name}=${access_cookie?.value}`,
// "Content-Type": "application/json",
// },
// cache: "no-store",
// },
// );

// if (!response.ok) {
// return new NextResponse(`${response.statusText}`, {
// status: response.status,
// });
// }

// cookies().delete("access_token");
// cookies().delete("refresh_token");
// return new NextResponse("회원 탈퇴 성공", {
// status: 200,
// });
}
Loading

0 comments on commit 87b0581

Please sign in to comment.