-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
24 lines (20 loc) · 789 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
24 lines (20 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Protect /admin/* routes
if (pathname.startsWith('/admin')) {
// Check for api key in cookie (set by login) or let client-side handle it
// We do a lightweight check: if no agentrel_session cookie, redirect to login
// Full role check is done client-side on each admin page
const sessionCookie = request.cookies.get('agentrel_session')
if (!sessionCookie) {
const loginUrl = new URL('/auth/login', request.url)
loginUrl.searchParams.set('redirect', pathname)
return NextResponse.redirect(loginUrl)
}
}
return NextResponse.next()
}
export const config = {
matcher: ['/admin/:path*'],
}