You should be able to dictate the redirection using withAuth's callbacks--> authorize False. It should not strictly be a redirect to Login #4105
Replies: 2 comments 2 replies
-
Agree with this, hopefully this is coming back around. I have our application separate into an /authenticated and /home folder and if the token exists but the permissions the user has does not allow them to view the route they are trying to go to, I would rather send them to a page that states that than all the way back to the login page where they have to be authenticated again. |
Beta Was this translation helpful? Give feedback.
-
This would be nice. To get this functionality with the current state of next-auth, I use the Here is my withAuth: export default withAuth(
function middleware(req) {
if (req.nextauth.token?.role !== 'authorizedUser') {
// redirect to a not authorized page
return new NextResponse('not authorized')
}
},
{
callbacks: {
authorized: ({ token }) => !!token,
},
secret: process.env.NEXTAUTH_SECRET,
},
) Here is my NextAuth jwt callback: callbacks: {
async jwt({ token }) {
const isAuthorized = await checkIfAuthorized(token)
if (isAuthorized) {
token.role = 'authorizedUser'
}
return token
},
}, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Description 📓
At the moment, returning False only redirects to the Login page; there is no other choice on what returning False can do.
I think that's completely limiting the use case of this great Middleware feature. Perhaps you would want to redirect to a page of choice that says "You are not authorized to view this page".
Suggestion. Maybe adding a redirect property to the pages option would suffice:
How to reproduce ☕️
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Beta Was this translation helpful? Give feedback.
All reactions