Files
todo/services/nextjs/middleware.ts
Mayne0213 471cde3986 FEAT(app): add password protection
- Add authentication feature
- Enable password login
2025-11-24 23:18:27 +09:00

52 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { isAuthenticated, createUnauthorizedResponse } from '@/shared/lib/auth'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// 공개 경로 (인증 불필요)
const publicPaths = [
'/api/auth/password', // 비밀번호 인증 API
'/api/health', // 헬스체크
]
// 공개 경로는 통과
if (publicPaths.some(path => pathname.startsWith(path))) {
return NextResponse.next()
}
// API 라우트 보호
if (pathname.startsWith('/api/')) {
if (!isAuthenticated(request)) {
return createUnauthorizedResponse()
}
return NextResponse.next()
}
// 페이지 라우트 보호
if (pathname === '/' || pathname.startsWith('/app')) {
if (!isAuthenticated(request)) {
// 인증되지 않은 경우, 클라이언트에서 처리하도록 그대로 진행
// (PasswordModal이 표시됨)
return NextResponse.next()
}
return NextResponse.next()
}
return NextResponse.next()
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}