NextAuth Session Not Persisting After Refresh
NextAuth sessions don't persist after page refresh or browser restart. Users are logged out when they refresh, despite having valid tokens. Session data is lost.
Session persistence fails when session adapter isn't configured, JWT expires immediately, or cookie settings don't allow persistence.
Error Messages You Might See
Common Causes
- No session adapter configured (defaults to JWT, not persisted in database)
- JWT maxAge set too low or 0, expiring immediately
- Session cookie sameSite: 'Lax' blocking cross-site persistence
- Database session table not created or inaccessible
- Cookie secure flag preventing HTTP localhost testing
How to Fix It
Add session adapter: For persistent sessions, use database adapter:adapter: PrismaAdapter(prisma),
session: { strategy: 'database' }
Or extend JWT: If using JWT, increase maxAge and set callbacks:jwt: { maxAge: 30 * 24 * 60 * 60 }, // 30 days
Check cookie settings: Ensure sameSite: 'Lax' or 'None', secure only in production (HTTPS).
Test with /api/auth/session: Call this route to verify session is persisted. Should return user object after login.
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
Should I use JWT or database sessions?
JWT: stateless, scalable, good for APIs. Database: stateful, can revoke instantly, better for web apps. Choose based on needs.
How long should session maxAge be?
30 days is common for remember-me. 1 day for security-sensitive apps. Set in strategy-specific config.
Why is session undefined on localhost?
Check secure: false in development. Cookies won't persist with secure: true on HTTP localhost. Use conditional in config.