Replies: 1 comment 1 reply
-
You can use the authorized callback passed to the
import { withAuth } from "next-auth/middleware"
export default withAuth({
callbacks: {
authorized: ({ req, token }) => {
const path = req.nextUrl.pathname;
// Check if the middleware is processing the
// route which requires a specific role
if (path.startsWith("/admin")) {
return token?.role === "admin";
}
// By default return true only if the token is not null
// (this forces the users to be signed in to access the page)
return token !== null;
}
}
})
// Define paths for which the middleware will run
export const config = {
matcher: [
"/profile/:path*",
"/admin/:path*"
]
} This will make both |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to do role based permissions in middleware?
I'm asking this because I was hoping that there would be a way to call getSession in middleware.
Otherwise I would need to implement a helper function
hasPermissions(req)
and call it in every single file and I'm trying to avoid thatThanks
Beta Was this translation helpful? Give feedback.
All reactions