NextAuth.js Infinite Redirect Loop on Callback
Your NextAuth.js application enters an infinite redirect loop when users attempt to authenticate. Users are redirected back to the login/callback page repeatedly, preventing successful authentication completion.
This typically occurs after the OAuth provider returns the authorization code, and NextAuth attempts to redirect the user to the callback URL specified in your configuration.
Error Messages You Might See
Common Causes
- Callback URL mismatch between NextAuth config and OAuth provider settings (trailing slashes, protocol differences)
- Circular redirect in the authorize callback or jwt callback functions
- Session validation failing silently, causing re-auth attempts
- Incorrect NEXTAUTH_URL environment variable or missing NEXTAUTH_SECRET
- Database session adapter connection issues preventing session creation
How to Fix It
Verify callback URLs: Ensure your OAuth provider (GitHub, Google, etc.) has the exact callback URL registered: http://localhost:3000/api/auth/callback/github
Check NextAuth config: Verify NEXTAUTH_URL matches your deployment URL exactly. In development use http://localhost:3000.
Debug redirect logic: Add logging in your callbacks to trace the redirect path. Check for infinite loops in redirect conditions.
Session persistence: If using a database adapter, verify the database connection and that session tables are properly created.
Real developers can help you.
You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.
Get HelpFrequently Asked Questions
How do I debug NextAuth redirects?
Enable debug mode in NextAuth config: debug: true. Check browser network tab for redirect chain and NextAuth logs in console.
Why does it work in dev but not production?
Production requires NEXTAUTH_URL set to your domain and NEXTAUTH_SECRET generated via openssl rand -base64 32.
What if I'm using a database adapter?
Ensure your database is accessible and tables are created via prisma migrate. Test connection before deploying.