Logging Out with the Application Load Balancer and Authentication Using Cognito

Asked 2 months ago, Updated 2 months ago, 379 views

Cognito wants to implement the behavior of logging out and redirecting to the login screen with the authentication feature implemented on the application load balancer (hereafter ALB), but has a CORS error.

ALB distributes requests to EC2 instances.This time, we implemented authentication functionality in Cognito in ALB.If you access the instance without logging in, the login screen created by Cognito will be displayed, and if you enter your login information, the screen you want to display will be displayed.
Then, in order to implement the logout feature, we implemented the ability to respond to the deletion of session cookies stored in the browser and redirect response to logout endpoints in the server-side program.

After logging in, place the logout button and press the button to call the server-side program, log out, and display the login screen further.

A CORS error occurs when attempting to redirect to a logout endpoint.I think this is because Cognito's domain is different from the one on the screen after logging in.I used no-cors because fetch API calls server-side programs, but in that case no response data will be returned, so I thought it would be necessary to set it up like Access-Control-Allow-Origin instead of no-cors, but there was no site that mentioned that.

javascript amazon-elb authentication-authorization

2023-01-05 10:15

1 Answers

I understand that fetch API sends request to Cognito logout endpoint.


Instead of sending the request via fetch API, try to redirect it by passing location.href() with the appropriate parameters.One of the parameters, redirect_uri, is the URL you want to view after logging out.

I want to log out and see more login screens

First of all, in order to understand how it works, redirect_uri should be checked to see if logout can be implemented by specifying the URL of the page that does not require authentication.Then, if you want the login screen to appear automatically, you can automatically redirect it to the URL of the app that needs authentication, and ALB will automatically redirect it to the Hosted UI of Cognito.

2023-01-05 19:46

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.