|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | +import type { NextRequest } from 'next/server'; |
| 3 | +import { cookies } from 'next/headers'; |
| 4 | + |
| 5 | +export function middleware(request: NextRequest) { |
| 6 | + const getCookie = (key: string) => { |
| 7 | + return cookies().get(key)?.value; |
| 8 | + }; |
| 9 | + const token = getCookie('access_token'); |
| 10 | + const pathname = request.nextUrl.pathname; |
| 11 | + // API 라우트나 정적 파일, 특정 정적 자원에 대한 요청을 건너뛰고, 페이지 요청에만 미들웨어를 적용합니다. |
| 12 | + if ( |
| 13 | + pathname.startsWith('/api') || |
| 14 | + pathname.includes('.') || |
| 15 | + pathname.startsWith('/_next/static/') || |
| 16 | + pathname.startsWith('/favicon') || |
| 17 | + pathname === '/manifest.json' || |
| 18 | + pathname === '/firebase-messaging-sw.js' || |
| 19 | + pathname.startsWith('/icons/') |
| 20 | + ) { |
| 21 | + return NextResponse.next(); |
| 22 | + } |
| 23 | + console.log('middleware', pathname); |
| 24 | + |
| 25 | + if ( |
| 26 | + !token && |
| 27 | + pathname !== '/sign' && |
| 28 | + pathname !== '/signin' && |
| 29 | + pathname !== '/signup' && |
| 30 | + pathname !== '/find' && |
| 31 | + pathname !== '/search' && |
| 32 | + pathname !== '/' |
| 33 | + ) { |
| 34 | + return NextResponse.redirect(new URL('/sign', request.url)); |
| 35 | + } |
| 36 | + |
| 37 | + return NextResponse.next(); |
| 38 | +} |
0 commit comments