29 lines
777 B
TypeScript
29 lines
777 B
TypeScript
import { NextResponse } from 'next/server'
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const { pathname } = request.nextUrl
|
|
|
|
// Check if the path is under /admin
|
|
if (pathname.startsWith('/admin')) {
|
|
// Allow access to login page
|
|
if (pathname === '/admin/login' || pathname.startsWith('/admin/api')) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
// Check for admin session cookie
|
|
const adminSession = request.cookies.get('admin_session')?.value
|
|
|
|
if (!adminSession || adminSession !== 'authenticated') {
|
|
return NextResponse.redirect(new URL('/admin/login', request.url))
|
|
}
|
|
}
|
|
|
|
return NextResponse.next()
|
|
}
|
|
|
|
// Match only admin paths
|
|
export const config = {
|
|
matcher: ['/admin/:path*'],
|
|
}
|