27 lines
749 B
TypeScript
27 lines
749 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const role = req.cookies.get('role')?.value;
|
|
|
|
const url = req.nextUrl.clone();
|
|
|
|
if (url.pathname === '/booking' || url.pathname === '/all-registration') {
|
|
if (role === 'admin' && url.pathname !== '/all-registration') {
|
|
url.pathname = '/all-registration';
|
|
return NextResponse.redirect(url);
|
|
}
|
|
if (role === 'user' && url.pathname !== '/booking') {
|
|
url.pathname = '/booking';
|
|
return NextResponse.redirect(url);
|
|
}
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// Optional: config where middleware runs
|
|
export const config = {
|
|
matcher: ['/booking', '/all-registration'],
|
|
};
|