From 6e57add690e5006d69ea698d00cb10f4ebec3cdd Mon Sep 17 00:00:00 2001 From: "chanki.kim" Date: Thu, 13 Feb 2025 23:02:42 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20:=20middleware=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/middleware.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 6a10901..1eac30d 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,21 +1,36 @@ import { NextRequest, NextResponse } from 'next/server'; import { cookies } from 'next/headers'; -const AFTER_LOGIN_DOMAIN = ['/mydashboard', '/dashboard/:path*', '/dashboard', '/mypage'] satisfies readonly string[]; +const AFTER_LOGIN_DOMAIN = ['/mydashboard', '/dashboard', '/mypage'] satisfies readonly string[]; const BEFORE_LOGIN_DOMAIN = ['/faq', '/privacy', '/login', '/signup', '/'] satisfies readonly string[]; export const middleware = async (request: NextRequest) => { const cookieStore = await cookies(); const accessToken = cookieStore.get('accessToken'); + + const pathname = request.nextUrl.pathname; + if (!accessToken?.value) { - if (AFTER_LOGIN_DOMAIN.includes(request.nextUrl.pathname)) return NextResponse.redirect(new URL('/login', request.url)); + if (AFTER_LOGIN_DOMAIN.some((path) => pathname.startsWith(path))) { + return NextResponse.redirect(new URL('/login', request.url)); + } } else { - if (BEFORE_LOGIN_DOMAIN.includes(request.nextUrl.pathname)) return NextResponse.redirect(new URL('/mydashboard', request.url)); + if (BEFORE_LOGIN_DOMAIN.includes(pathname)) { + return NextResponse.redirect(new URL('/mydashboard', request.url)); + } } return NextResponse.next(); }; export const config = { - matcher: '/:path*', + matcher: [ + { + source: '/:path*', + missing: [ + { type: 'header', key: 'next-router-prefetch' }, + { type: 'header', key: 'purpose', value: 'prefetch' }, + ], + }, + ], };