I`m trying to redirect a user to specific pages based on role, using the following code in the middleware.ts file
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { getToken } from 'next-auth/jwt'
export async function middleware(request: NextRequest) {
const token = await getToken({
req: request,
secret: process.env.JWT_SECRET
}) as any
if (token !== null && token.user.role === "admin") {
if (request.nextUrl.pathname.startsWith('/admin')) {
return NextResponse.next()
}
else {
return NextResponse.redirect(new URL('/admin', request.url))
}
}
}
/* export const config = {
matcher: [
// "/login",
// "/register",
// "/",
// "/admin"
],
} */
From what I understood from the docs it will run for all the routes, but I get this error :
Uncaught SyntaxError: Unexpected token '<
However, if I include the matcher in the middleware file, it works as intended:
export const config = {
matcher: [
"/login",
"/register",
"/",
"/admin"
],
}
Is there a fix for that error? Or just add all the pages to the matcher array?
