Next.js Discord

Discord Forum

Middleware all Pages inside Route Group

Unanswered
Hokkaido posted this in #help-forum
Open in Discord
HokkaidoOP
Hello everyone, I use the middleware of nextjs 14 for authentication purposes. As I understand it, I can use the matcher to trigger the middleware on private paths, for example dashboard or customers, where the middleware checks whether this user is authenticated. I have my private routes in a route group (platform) and would like to specify in the matcher that all pages in this route group trigger the middleware. Alternatively, I would have to enter each private route separately. Is that possible?

import { NextResponse } from 'next/server' import type { NextRequest } from 'next/server' // This function can be markedasyncif usingawaitinside export function middleware(request: NextRequest) { return NextResponse.redirect(new URL('/home', request.url)) } // See "Matching Paths" below to learn more export const config = { matcher: '(platform)/*', <----------- }

1 Reply