- Frontend:
http://localhost:5173(Vite dev server) - Backend:
http://localhost:5000(Express server) - Relationship: Cross-origin
- Cookie Settings:
SameSite=None; Secure(requires HTTPS or localhost exception)
- Frontend:
https://property-manager-ke.vercel.app - Backend: Same domain via rewrites (e.g.,
https://property-manager-ke.vercel.app/api/*) - Relationship: Same-origin
- Cookie Settings:
SameSite=Lax; Secure
The application automatically detects whether requests are cross-origin or same-origin and adjusts cookie settings accordingly:
SameSite=Lax; Secure; HttpOnly; Path=/
- ✅ Better CSRF protection
- ✅ Cookies sent on navigation and same-origin requests
- ❌ Cookies NOT sent on cross-origin POST requests
SameSite=None; Secure; HttpOnly; Path=/
- ✅ Cookies work across different domains/subdomains
- ✅ Enables API on separate domain (e.g.,
api.example.com) ⚠️ Requires HTTPS (or localhost for development)⚠️ Less CSRF protection (relies on CORS)
Example: property-manager-ke.vercel.app
- Frontend:
https://property-manager-ke.vercel.app - API:
https://property-manager-ke.vercel.app/api/* - Cookie Strategy:
SameSite=Lax✅ (automatically detected)
Example: Frontend and API on different subdomains
- Frontend:
https://app.example.com - API:
https://api.example.com - Required Change: The API will detect cross-origin and use
SameSite=Noneautomatically - Requirements:
- Both domains must use HTTPS
- CORS headers must allow credentials
- Cookie domain should be set to
.example.com(requires code change)
Example: Frontend and API on different domains
- Frontend:
https://myapp.com - API:
https://myapi.io - Required Change: Same as Scenario 2, but cannot share cookies easily
- Recommendation: Use Authorization header with JWT tokens instead of cookies
- Open browser DevTools → Network tab
- Trigger authentication
- Check the request to
/api/auth/set-session - Look at the
Originheader:- Same as host: Same-origin →
SameSite=Lax✅ - Different from host: Cross-origin →
SameSite=Nonerequired
- Same as host: Same-origin →
Before deploying with custom domains:
- Verify frontend and API are on the same domain
- If using subdomains, test cookie behavior
- Check browser DevTools → Application → Cookies
- Confirm
SameSiteattribute matches your setup - Test authentication flow end-to-end
- Check for "Cookie blocked" warnings in console
Symptom: 401 Unauthorized errors after successful login
Possible Causes:
-
Cross-origin with SameSite=Lax: API is on different domain/subdomain
- Solution: API automatically detects this, but verify HTTPS is enabled
-
Missing Secure flag: Browser requires HTTPS for
SameSite=None- Solution: Ensure production uses HTTPS (Vercel does this automatically)
-
CORS misconfiguration: Credentials not allowed
- Solution: Verify CORS headers include
Access-Control-Allow-Credentials: true
- Solution: Verify CORS headers include
-
Third-party cookie blocking: Browser privacy settings
- Solution: Use same-origin deployment (Scenario 1) or switch to tokens
Symptom: Browser console shows "Cookie has been blocked" warnings
Cause: Cross-origin cookies with SameSite=Lax
Solution: The application should auto-detect and use SameSite=None, but you can force it by ensuring the Origin header is present in requests.
To enable detailed cookie debugging, set:
DEBUG=trueOr for production debugging:
NODE_ENV=developmentThis will log:
- Request origin detection
- Cross-origin vs same-origin determination
- Cookie configuration being applied
- Whether Secure flag is set
- Prefer same-origin deployment (Scenario 1) when possible
- Always use HTTPS in production
- Keep HttpOnly flag on authentication cookies
- Enable CORS only for trusted origins
- Monitor cookie behavior after any domain changes
- Consider using refresh token rotation for enhanced security
Cookie settings are configured in:
api/auth/set-session.ts- Where cookies are initially set- Logic automatically detects cross-origin vs same-origin
- Applies appropriate
SameSiteandSecureflags