From 57c3ead828805d846fb4334719c8371c030375d2 Mon Sep 17 00:00:00 2001 From: hamo-o Date: Tue, 17 Oct 2023 17:48:45 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20?= =?UTF-8?q?=EC=8B=9C=20user=20cookie=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/auth/checkSignIn.ts | 13 ++++++++++--- src/apis/auth/checkSignUp.ts | 19 +++++++------------ src/middleware.ts | 4 +--- src/pages-edit/mypage/SettingPage.tsx | 3 ++- src/utils/cookie.ts | 6 ++++++ 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/apis/auth/checkSignIn.ts b/src/apis/auth/checkSignIn.ts index 078d228..c7cc70b 100644 --- a/src/apis/auth/checkSignIn.ts +++ b/src/apis/auth/checkSignIn.ts @@ -3,12 +3,19 @@ import type { NextRequest } from "next/server"; import { checkSignUp } from "./checkSignUp"; export async function checkSignIn(request: NextRequest) { - const token = request.cookies.get("accessToken")?.value; + const token = request.cookies.has("accessToken"); const url = request.nextUrl.clone(); if (token) { - if ("/((?!auth).*)") { - return checkSignUp(request); + if (!request.nextUrl.pathname.includes("/auth")) { + const userToken = request.cookies.has("user"); + if (userToken) { + return NextResponse.next(); + } + // 최초진입인 경우 + 회원가입을 하지 않은 경우 + 하다만 경우 + else { + return checkSignUp(request); + } } else { return NextResponse.next(); } diff --git a/src/apis/auth/checkSignUp.ts b/src/apis/auth/checkSignUp.ts index 7a97547..5e7d5da 100644 --- a/src/apis/auth/checkSignUp.ts +++ b/src/apis/auth/checkSignUp.ts @@ -5,21 +5,16 @@ import { checkUser } from "./checkUser"; export async function checkSignUp(request: NextRequest) { const user = await checkUser(request); const url = request.nextUrl.clone(); + const response = NextResponse.next(); if (user.name && user.schoolType) { - return NextResponse.next(); + response.cookies.set({ name: "user", value: "true", path: "/" }); + return response; } else if (user.name) { - if ( - url.pathname !== "/auth/organization" && - url.pathname !== "/auth/default" - ) { - url.pathname = "/auth/organization"; - return NextResponse.redirect(url); - } else return NextResponse.next(); + url.pathname = "/auth/organization"; + return NextResponse.redirect(url); } else { - if (url.pathname !== "/auth/default") { - url.pathname = "/auth/default"; - return NextResponse.redirect(url); - } else return NextResponse.next(); + url.pathname = "/auth/default"; + return NextResponse.redirect(url); } } diff --git a/src/middleware.ts b/src/middleware.ts index 198731f..cb316ef 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,16 +1,14 @@ import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { checkSignIn } from "./apis/auth/checkSignIn"; -import { qFeedAxios } from "./apis/axios"; export async function middleware(request: NextRequest) { - const { pathname } = request.nextUrl; const token = request.cookies.get("accessToken")?.value; const requestHeaders = new Headers(request.headers); requestHeaders.set("Authorization", `Bearer ${token}`); - if (pathname.match("/((?!account).*)")) { + if (!request.nextUrl.pathname.includes("/account")) { return await checkSignIn(request); } diff --git a/src/pages-edit/mypage/SettingPage.tsx b/src/pages-edit/mypage/SettingPage.tsx index 9c426a0..b1b8ef9 100644 --- a/src/pages-edit/mypage/SettingPage.tsx +++ b/src/pages-edit/mypage/SettingPage.tsx @@ -7,7 +7,7 @@ import { useRouter } from "next/navigation"; import NavigationTopBack from "src/components/navigations/NavigationTopBack"; import Flex from "src/components/common/Flex"; import Text from "src/components/common/Text"; -import { deleteCookie } from "src/utils/cookie"; +import { deleteCookie, deleteUser } from "src/utils/cookie"; import useDeleteMeMutation from "src/hooks/account/useDeleteMeMutation"; export default function SettingPage() { @@ -31,6 +31,7 @@ export default function SettingPage() { onClick={() => { deleteMeMutation.mutate(); deleteCookie(); + deleteUser(); router.push("/account"); }} > diff --git a/src/utils/cookie.ts b/src/utils/cookie.ts index 4ca135c..c5d19a9 100644 --- a/src/utils/cookie.ts +++ b/src/utils/cookie.ts @@ -17,3 +17,9 @@ export const deleteCookie = () => { path: "/" }); }; + +export const deleteUser = () => { + return cookies.remove("user", { + path: "/" + }); +};