Skip to content

Commit

Permalink
Merge pull request #357 from TripInfoWeb/dev_auth
Browse files Browse the repository at this point in the history
Dev auth
  • Loading branch information
ssssksss authored Sep 21, 2024
2 parents 74d1fb5 + a083adf commit f2c111f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 48 deletions.
22 changes: 14 additions & 8 deletions src/app/api/auth/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fetchWithAuth } from "@/utils/fetchWithAuth";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";

Expand Down Expand Up @@ -56,15 +57,20 @@ export async function DELETE(request: NextRequest) {
return new NextResponse("Refresh token not found", { status: 401 });
}

// 사용자 정보 조회 API
const response = await fetch(`${process.env.BACKEND_URL}/api/users`, {
method: "DELETE",
headers: {
Cookie: `${access_cookie?.name}=${access_cookie?.value}`,
"Content-Type": "application/json",
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",
},
cache: "no-store",
});
);

if (!response.ok) {
return new NextResponse(`${response.statusText}`, { status: response.status });
Expand Down
19 changes: 7 additions & 12 deletions src/app/mypage/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ export const metadata: Metadata = {
};

async function getUserInfo() {

const access_token = cookies().get("access_token");
const refresh_token = cookies().get("refresh_token");
const response = await fetchWithTokenRefreshSSR(
{
url: `${process.env.BACKEND_URL}/api/users/info`,
accessToken: access_token,
refreshToken: refresh_token,
});

if (!response.ok) {
// This will activate the closest 'error.tsx' Error Boundary.
throw new Error(response.statusText);
}
const response = await fetchWithTokenRefreshSSR<userResponseDto>({
url: `${process.env.BACKEND_URL}/api/users/info`,
accessToken: access_token,
refreshToken: refresh_token,
});

return response.json() as Promise<userResponseDto>;
return response;
}


Expand Down
7 changes: 5 additions & 2 deletions src/app/mypage/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MyProfileContainer from "@/containers/mypage/MyProfileContainer";
import { userResponseDto } from "@/types/UserDto";
import { fetchWithTokenRefreshSSR } from "@/utils/getNewAccessTokenAndRerequest";
import { Metadata } from "next";
import { cookies } from "next/headers";
Expand All @@ -9,19 +10,21 @@ export const metadata: Metadata = {
};

async function getUserInfo() {
return fetchWithTokenRefreshSSR({
const response = fetchWithTokenRefreshSSR<userResponseDto>({
accessToken: cookies().get("access_token"),
refreshToken: cookies().get("refresh_token"),
url: `${process.env.BACKEND_URL}/api/users/info`,
});

return response;
}

export default async function page() {
const userInfo = await getUserInfo();

return (
<div className={"min-h-[calc(100vh-25rem)] w-full px-[.5rem] pb-[2.5rem]"}>
<MyProfileContainer userInfo={await userInfo.json()} />
<MyProfileContainer userInfo={userInfo} />
</div>
);
}
5 changes: 3 additions & 2 deletions src/app/support/qna/detail/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SupportQnADetailEditContainer from "@/containers/support/qna/SupportQnADetailEditContainer";
import { QnADetailType } from "@/types/QnADto";
import { fetchWithTokenRefreshSSR } from "@/utils/getNewAccessTokenAndRerequest";
import { cookies } from "next/headers";

Expand All @@ -22,7 +23,7 @@ async function fetchData(id: number) {
const accessToken = cookies().get("access_token");
const refreshToken = cookies().get("refresh_token");

return fetchWithTokenRefreshSSR({
return await fetchWithTokenRefreshSSR<QnADetailType>({
url: `${process.env.BACKEND_URL}/api/qna/${id}`,
accessToken: accessToken,
refreshToken: refreshToken,
Expand All @@ -39,7 +40,7 @@ export default async function Page({ params: { id } }: Props) {

return (
<main className="mb-8 w-full">
<SupportQnADetailEditContainer data={await data.json()} />
<SupportQnADetailEditContainer data={data} />
</main>
);
}
9 changes: 5 additions & 4 deletions src/components/auth/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const SignIn = () => {
카카오로 로그인
</span>
</Link>
<Link
{/* <Link
className={
"relative mb-[3rem] flex h-[2.875rem] w-full items-center justify-center rounded-3xl outline outline-[1px] outline-offset-[-1px] outline-gray3"
"relative mb-[.75rem] flex h-[2.875rem] w-full items-center justify-center rounded-3xl outline outline-[1px] outline-offset-[-1px] outline-gray3"
}
href="/api/auth/google"
>
Expand All @@ -58,8 +58,9 @@ const SignIn = () => {
/>
</div>
<span className="text-sm font-semibold text-black">구글로 로그인</span>
</Link>
<div className={"relative flex w-full justify-center"}>
</Link> */}

<div className={"relative flex w-full justify-center mt-[3rem]"}>
<div className={"absolute top-[-.5rem] flex flex-col items-center"}>
<div
className={
Expand Down
6 changes: 3 additions & 3 deletions src/components/auth/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const SignUp = () => {
카카오로 1초만에 시작하기
</span>
</Link>
<Link
{/* <Link
className={
"relative mb-[3rem] flex h-[2.875rem] w-full items-center justify-center rounded-3xl outline outline-[1px] outline-offset-[-1px] outline-gray3"
"relative mb-[.75rem] flex h-[2.875rem] w-full items-center justify-center rounded-3xl outline outline-[1px] outline-offset-[-1px] outline-gray3"
}
href="/api/auth/google"
>
Expand All @@ -60,7 +60,7 @@ const SignUp = () => {
<span className="text-sm font-semibold text-black">
구글로 1초만에 시작하기
</span>
</Link>
</Link> */}
<div className={"relative flex w-full justify-center"}>
<p
className={
Expand Down
2 changes: 1 addition & 1 deletion src/components/mypage/MyPageUserImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IMyPageUserImage {
dragAndDrop: dragAndDropProps;
imageBase64Data: string;
userImageUrl: string;
userSex: string;
userSex: string | null;
isModalOpen: boolean;
closeCropModal: () => void;
onChangeImageUrl: (_: string) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/containers/mypage/MyPageUserImageContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useState } from "react";

interface IMyPageUserImageContainer {
userImageUrl: string;
userSex: string;
userSex: string | null;
}

const MyPageUserImageContainer = (props: IMyPageUserImageContainer) => {
Expand Down
28 changes: 16 additions & 12 deletions src/types/UserDto.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Request 요청 결과 Dto
interface UserImage {
id: number;
address: string;
createdDate: string;
}

export interface userResponseDto {
age: number; // 연도
email: string;
id: number;
isAdmin: boolean;
userStatus: string;
userImage: UserImage;
nickname: string;
phoneNumber: string;
sex: string; // "male, female"
userImage: {
id: number;
address: string;
createdDate: string; // "2024-07-12",
};
userStatus: string; // "활성화" | "휴먼" | "삭제" | "관리자" | "";
}
age: number;
sex: string | null;
email: string;
provider: string;
phoneNumber: string | null;
createdAt: string;
isAdmin: boolean;
}
7 changes: 4 additions & 3 deletions src/utils/getNewAccessTokenAndRerequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ interface IFetchWithTokenRefreshSSR {
next?: NextFetchRequestConfig;
}

export async function fetchWithTokenRefreshSSR({
export async function fetchWithTokenRefreshSSR<T>({
accessToken,
refreshToken,
url,
method,
cache,
contentType,
next,
}: IFetchWithTokenRefreshSSR) {
}: IFetchWithTokenRefreshSSR): Promise<T> {
let response = await fetch(url, {
method: method || "GET",
headers: {
Expand Down Expand Up @@ -103,5 +103,6 @@ export async function fetchWithTokenRefreshSSR({
throw new Error("API 요청에 실패했습니다.");
}

return response;
const data = await response.json();
return data as T;
}

0 comments on commit f2c111f

Please sign in to comment.