-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (31 loc) · 1.04 KB
/
middleware.ts
File metadata and controls
37 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// 인증이 필요하지 않은 경로 목록
const publicPaths = ['/', '/api/auth/login']
export function middleware(request: NextRequest) {
const accessToken = request.cookies.get('accessToken')?.value
const { pathname } = request.nextUrl
// public 경로는 체크하지 않음
if (publicPaths.includes(pathname)) {
return NextResponse.next()
}
// 토큰이 없으면 로그인 페이지로 리다이렉트
if (!accessToken) {
const url = new URL('/', request.url)
return NextResponse.redirect(url)
}
return NextResponse.next()
}
// 미들웨어가 적용될 경로 설정
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}