- Newest
- Most votes
- Most comments
To properly log out from an ALB-Cognito setup with an EC2 instance running a React application, you need to follow a few steps:
-
Use the Cognito logout endpoint: Direct your users to the Cognito
/logoutendpoint when they want to sign out. This endpoint is a redirection endpoint that signs out the user and redirects them to either an authorized sign-out URL for your app client or to the/loginendpoint. -
Configure the logout URI: In your Cognito app client settings, add an "Allowed sign-out URL" that points to your React application's logout page. This is the page where users will be redirected after signing out from Cognito.
-
Implement client-side logout: In your React application, create a logout function that does the following:
- Call the Cognito
/logoutendpoint with the appropriate parameters, including yourclient_idandlogout_uri. - Clear any local storage or state that contains user information.
- Optionally, you can attempt to clear cookies, but note that this may not always work as expected when running on AWS due to security restrictions.
- Call the Cognito
-
Handle the redirect: On your logout page (the one specified in the
logout_uri), implement logic to complete the logout process. This may include clearing any remaining local data and redirecting the user to a signed-out state of your application. -
Consider SAML logout: If you're using SAML authentication, be aware that Cognito supports SAML 2.0 single sign-out (SLO). This allows users to be signed out from their SAML identity providers when they sign out from the user pool.
Remember that the Cognito logout process primarily invalidates the user's tokens on the server-side. Client-side actions like clearing cookies are supplementary and may have limitations when running in a cloud environment.
If you're still experiencing issues with cookie deletion on AWS, you may need to review your application's security settings, ensure that your React app has the necessary permissions to modify cookies, and consider using secure, HTTP-only cookies for sensitive information.
Lastly, always test your logout flow thoroughly to ensure it works as expected in your production environment.
Sources
The managed login sign-out endpoint: /logout - Amazon Cognito
GlobalSignOut - Amazon Cognito User Pools
Signing out SAML users with single sign-out - Amazon Cognito
Relevant content
asked 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 7 months ago
