Replies: 11 comments 8 replies
-
In the meantime, you can create a switch and use path-to-regexp to match the paths/urls |
Beta Was this translation helpful? Give feedback.
-
Barely tested example: type Middleware = {
matcher: string,
handler: NextMiddleware
}
function handlePaths(middlewares: Middleware[]): NextMiddleware {
return async function(req, ev) {
const path = req.nextUrl.pathname
const middleware = middlewares.find(m => pathToRegexp(m.matcher).test(path))
if (middleware) {
return middleware.handler(req, ev)
}
// if there's no middleware just continue
return NextResponse.next()
}
} Then use it as middleware export const middleware: NextMiddleware = handlePaths([
{
matcher: '/sign-in',
// redirects route on `/sign-in` => `/login`
handler: (req) => NextResponse.redirect(new URL('/login', req.url)),
},
// fallback, can be ignored in this case
{
matcher: '/:path*',
handler: async (req) => NextResponse.next()
},
]) |
Beta Was this translation helpful? Give feedback.
-
@rawnly For example: const pathHandlers = new Map([
['/sign-in', (req) => NextResponse.redirect(new URL('/login', req.url))],
['/:path*', async (req) => NextResponse.next()],
]); Instead of defining the handlePaths middleware function inline, you could define it as a separate, named function. This would make the code easier to read and understand, and would also make it easier to test and reuse the function in other parts of your application function handlePaths(pathHandlers: Map<string, RequestHandler>): NextMiddleware {
return (req, res) => {
// Look up the handler function for the current request path
const handler = pathHandlers.get(req.url.pathname);
if (handler) {
// If a handler is found, invoke it and return its response
return handler(req, res);
} else {
// If no handler is found, pass the request on to the next middleware in the pipeline
return NextResponse.next();
}
};
} |
Beta Was this translation helpful? Give feedback.
-
@mertcanaltin yes mine was just a quick example ✌️ |
Beta Was this translation helpful? Give feedback.
-
@mertcanaltin can you please provide the full |
Beta Was this translation helpful? Give feedback.
-
@omar2205 it should be the same as mine, he only changed the core of the function 😉 |
Beta Was this translation helpful? Give feedback.
-
It just, I had some type issues. |
Beta Was this translation helpful? Give feedback.
-
@rawnly any ideas on how to add |
Beta Was this translation helpful? Give feedback.
-
@omar2205 Since import { withAuth } from 'next-auth/middleware'
const pathHandlers = new Map([
['/sign-in', (req) => NextResponse.redirect(new URL('/login', req.url))],
['/dashboard', withAuth(function (req) { ... })],
['/:path*', async (req) => NextResponse.next()],
]); |
Beta Was this translation helpful? Give feedback.
-
@rawnly import { withAuth } from 'next-auth/middleware'
const pathHandlers = new Map([
['/sign-in', (req) => NextResponse.redirect(new URL('/login', req.url))],
['/dashboard', withAuth(function (req) {
console.log(req)
}, {
callbacks: {
authorized: ({ token }) => token?.role === "admin",
},
})
],
['/:path*', async (req) => NextResponse.next()],
]); |
Beta Was this translation helpful? Give feedback.
-
@balazsorban44 I'm sorry, but this is still a feature request. |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
It would be awesome to have something like this
Describe the solution you'd like
Adding a middleware array and mapping paths with a handler
Describe alternatives you've considered
Ugly checking for paths in the current single middleware function
Beta Was this translation helpful? Give feedback.
All reactions