-
Infinite redirect loop with custom sign in page and credentials provider. I'm using the next auth middleware to redirect user to sign in page like this. pages/_middleware.ts: This properly works with the built in sign in page and issue only occurs with custom sign in page. URL looks like this after redirect:
Is there any workaround for this issue? has anyone faced similar issue? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
pages/_middleware.ts:
|
Beta Was this translation helpful? Give feedback.
-
Also, there's a note buried in the docs that say the middleware option only works if you're using the jwt session option. I was using the database session option. If you're using a database, you'll get an infinite redirect if you use the middleware #5392 |
Beta Was this translation helpful? Give feedback.
-
this is a proper example of next js full stack with next auth and it also cover cors `import { NextResponse } from 'next/server'; const corsHeaders = { export default withAuth( export const config = { |
Beta Was this translation helpful? Give feedback.
-
I had this problem, and the solution for me was to update my middleware. I (unknowingly) had it configured with a authorized-callback like so: export default withAuth(
function middleware(request: NextRequestWithAuth) {
// ...
},
{
callbacks: {
authorized: ({ token }) => !!token
},
}
) ... which is something I found worked (with the default signin-page), after following a tutorial. With a custom signin-page, however, it intercepted the request to the signin page, and said "hey, you're not authorized, you need to be redirected to the signin-page!", over and over. I solved it like this: export default withAuth(
function middleware(request: NextRequestWithAuth) {
// ...
},
{
callbacks: {
authorized: ({ token, req }) => {
// Always allow access to login page
if (req.nextUrl.pathname.startsWith('/login')) return true
return !!token
},
},
}
) ... which basically just says "intercept all calls, just like you did before, excep calls to '/login', since you don't need to be logged in to go there". |
Beta Was this translation helpful? Give feedback.
pages/_middleware.ts: