dynamic routes in middleware with next-auth

so i want to do dynamic routes using next-auth with the config varibe that middleware offers This is what i have defined
export { default } from "next-auth/middleware"

export const config = { matcher: ["/[id]/:path*", "/recipes/[id]/:path*"] }
export { default } from "next-auth/middleware"

export const config = { matcher: ["/[id]/:path*", "/recipes/[id]/:path*"] }
i have tried doing something like this
export { default } from "next-auth/middleware"

export const config = { matcher: ["/(.*)/:path*", "/recipes/(.*)/:path*"] }
export { default } from "next-auth/middleware"

export const config = { matcher: ["/(.*)/:path*", "/recipes/(.*)/:path*"] }
i also tried defining it myself using a function but that didn't really work
Solution:
Ok ,then you can just expand on my example like so
Jump to solution
5 Replies
JulieCezar
JulieCezar2y ago
This one works for me
// middleware.ts
import { withAuth } from "next-auth/middleware";

export default withAuth(
// `withAuth` augments your `Request` with the user's token.
function middleware(req) {
}
);

// See "Matching Paths" below to learn more
export const config = {
matcher: ["/admin/:path*", "/student/:path*", "/teacher/:path*"],
};
// middleware.ts
import { withAuth } from "next-auth/middleware";

export default withAuth(
// `withAuth` augments your `Request` with the user's token.
function middleware(req) {
}
);

// See "Matching Paths" below to learn more
export const config = {
matcher: ["/admin/:path*", "/student/:path*", "/teacher/:path*"],
};
looking at your example you have conflicting matchers... This one matches all routes except /
export const config = { matcher: ["/:path*"] }
export const config = { matcher: ["/:path*"] }
This one matches all recipe routes
export const config = { matcher: ["/recipes/:path*"] }
export const config = { matcher: ["/recipes/:path*"] }
Jacob
JacobOP2y ago
yeh right that makes sense, so i was thinking more dynamic routes as a recipe will look like this /recipes/test123 and then a user can edit it when they go to /recipes/test123/edit which is how i got thise here: /recipes/[id]/:path* as i don't want to exclude the recipe itself but just the editing page
Solution
JulieCezar
JulieCezar2y ago
Ok ,then you can just expand on my example like so
JulieCezar
JulieCezar2y ago
export const config = { matcher: ["/recipes/:path/edit"] }
export const config = { matcher: ["/recipes/:path/edit"] }
this will match all routes like so /recipes/some-recipe-123/edit the * matches anything else after it meaning
export const config = { matcher: ["/recipes/:path*"] }
export const config = { matcher: ["/recipes/:path*"] }
will match /recipes/123 and recipes/123/edit and recipes/123/edit/123/something-else
Jacob
JacobOP2y ago
Awesome thank you
Want results from more Discord servers?
Add your server