- Newest
- Most votes
- Most comments
To securely log out from AWS Cognito Hosted UI in a SPA without CORS errors, you need to understand that the Cognito /logout endpoint is designed as a redirection endpoint, not an API endpoint for AJAX calls.
The recommended approach for logging out users from Cognito in a SPA is:
- Use a window redirect to the Cognito
/logoutendpoint. This is the standard approach because:
- The logout endpoint is specifically designed to work with redirects
- It properly invalidates tokens on the server-side
- It clears session cookies
For example:
window.location.href = "https://mydomain.auth.us-east-1.amazoncognito.com/logout?client_id=your_client_id&logout_uri=https://your-app.com/logout-page";
The logout_uri parameter should point to a page in your application that's configured as an authorized sign-out URL in your Cognito app client settings.
On your logout page, you should:
- Clear any local storage or state containing user information
- Reset any application cookies to expired timestamps
- Handle the UI transition to a logged-out state
For client logout landing pages, it's important to note that they must be unauthenticated. This means they cannot be behind an Application Load Balancer rule that requires authentication.
AJAX/XHR calls to the /logout endpoint aren't supported because:
- The endpoint doesn't support CORS for cross-domain requests
- The endpoint is designed for browser redirects to properly clear session state
- The logout process involves multiple redirects that can't be properly handled via AJAX
If you're experiencing issues with the window redirect approach, ensure that:
- Your
logout_uriis properly URL-encoded - The URI is registered as an allowed sign-out URL in your Cognito app client settings
- You're including the required
client_idparameter
This redirect-based approach is the standard pattern for OIDC providers, not just Cognito, and is the most secure way to handle logout in SPAs using external authentication providers.
Sources
How to logout from ALB-Cognito which is EC2 instance running React | AWS re:Post
The managed login sign-out endpoint: /logout - Amazon Cognito
Authenticate users using an Application Load Balancer - Elastic Load Balancing
