Users are redirected to sign-in page after logging in when accessing the profile page in production.
Missing or incorrect environment variables in production, specifically NEXTAUTH_URL and NEXTAUTH_SECRET.
✅ Updated cookie secure option to use process.env.NODE_ENV === "production" instead of hardcoded false
NEXTAUTH_URL=https://your-production-domain.com
NEXTAUTH_SECRET=your-secure-random-secret-here
JWT_SECRET=your-jwt-secret-hereIMPORTANT:
NEXTAUTH_URLmust be your actual production domain (e.g.,https://tarah.vercel.app)- DO NOT use
http://localhost:3000in production NEXTAUTH_SECRETshould be a long, random string (generate with:openssl rand -base64 32)
DATABASE_URL=your-neon-db-connection-stringUPLOADTHING_TOKEN=your-uploadthing-token-
Go to your Vercel Dashboard
-
Select your project
-
Go to Settings → Environment Variables
-
Add each variable:
- Variable name:
NEXTAUTH_URL - Value:
https://your-domain.vercel.app(your actual domain) - Environment: Production ✓
- Variable name:
-
Repeat for all variables above
-
Redeploy your application after adding variables
Vercel can automatically set NEXTAUTH_URL, but it's better to set it explicitly:
# Add this to your vercel.json if you want automatic domain detection
{
"env": {
"NEXTAUTH_URL": "${VERCEL_URL}"
}
}However, this doesn't work with custom domains. Best practice: Set it manually.
After deployment:
- ✅ Sign up with a new account
- ✅ Login with existing account
- ✅ Navigate to
/profile- should NOT redirect to sign-in - ✅ Check browser DevTools → Application → Cookies
- Should see
next-auth.session-token - Cookie should have
Secureflag in production
- Should see
Solution: Clear browser cookies and try again. Old insecure cookies may persist.
Solution: Make sure DATABASE_URL points to the correct production database.
Solution:
- Verify
NEXTAUTH_SECRETis the same across all environment variables - Check that cookies are not blocked by browser
- Ensure
NEXTAUTH_URLmatches your actual domain exactly (including https://)
-
NEXTAUTH_URLis set to production domain -
NEXTAUTH_SECRETis set (not empty) - All environment variables are in Production environment
- Application is redeployed after adding variables
- Browser cookies cleared for testing
- Session token cookie has
Secureflag
Your .env.local remains unchanged:
NEXTAUTH_URL=http://localhost:3000The code automatically uses secure: false for development and secure: true for production.