This project now supports Firebase session cookies as a more secure alternative to storing JWT tokens in localStorage. Session cookies provide better security against XSS attacks and are automatically sent with requests to your domain.
- User Login: When a user logs in with Firebase Auth, a JWT token is obtained
- Session Creation: The JWT token is sent to
/api/auth/sessionto create a secure session cookie - Cookie Storage: A secure HTTP-only cookie is set with the session data
- API Requests: Subsequent requests automatically include the session cookie
- HTTP-Only: Cookies cannot be accessed by JavaScript (prevents XSS attacks)
- Secure: Cookies are only sent over HTTPS in production
- SameSite: Set to 'strict' for CSRF protection
- Expiration: Session cookies expire after 5 days
- Domain Restriction: Only valid for your domain
The system supports both authentication methods:
- Primary: Session cookies (more secure)
- Fallback: JWT tokens in Authorization header (for compatibility)
POST /api/auth/session
Authorization: Bearer <jwt-token>
Creates a secure session cookie from a valid JWT token.
POST /api/auth/logout
Revokes refresh tokens and clears the session cookie.
// Create session cookie from JWT token
await apiClient.createSession(jwtToken);
// Clear session cookie
apiClient.clearSession();
// List all cookies (for debugging)
const cookies = apiClient.listAllCookies();The main application automatically creates session cookies when users log in:
onAuthStateChanged(auth, async (user) => {
if (user) {
const token = await user.getIdToken();
await apiClient.createSession(token);
} else {
apiClient.clearSession();
}
});Use the session test page to verify functionality:
- Navigate to
/session-test.html - Login with your Google account
- Test session cookie creation and API calls
- View cookie information
- Enhanced Security: HTTP-only cookies prevent XSS attacks
- Automatic Handling: Cookies are automatically sent with requests
- Better UX: No need to manually manage tokens
- CSRF Protection: SameSite attribute prevents cross-site attacks
- Compliance: Meets security best practices for web applications
The implementation is backward compatible:
- Existing JWT token authentication still works
- New session cookie authentication is preferred
- Gradual migration is possible
Session cookies are configured with the following settings:
- Expiration: 5 days
- HTTP-Only: true
- Secure: true (in production)
- SameSite: strict
- Path: /
These settings can be modified in the /api/auth/session endpoint.