feat: add middleware to protect admin routes with authentication check
This commit is contained in:
parent
05f1773d87
commit
7b6735be0e
24
middleware.ts
Normal file
24
middleware.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
|
||||
// Move accessToken to HttpOnly cookie in future for better security
|
||||
// Backend sets 'refreshToken' cookie on login; use it as auth presence
|
||||
const AUTH_COOKIES = ['refreshToken']
|
||||
|
||||
export function middleware(req: NextRequest) {
|
||||
const { pathname } = req.nextUrl
|
||||
|
||||
// Only guard admin routes
|
||||
if (pathname.startsWith('/admin')) {
|
||||
const hasAuthCookie = AUTH_COOKIES.some((name) => !!req.cookies.get(name)?.value)
|
||||
if (!hasAuthCookie) {
|
||||
const loginUrl = new URL('/login', req.url)
|
||||
return NextResponse.redirect(loginUrl)
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/admin/:path*'],
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user