gpt4 book ai didi

javascript - 无法从 React 应用程序中销毁 AWS Cognito session

转载 作者:行者123 更新时间:2023-12-05 00:29:30 34 4
gpt4 key购买 nike

我正在尝试通过调用他们的注销 endpoint 来注销使用 AWS Cognito 的应用程序。 .我没有使用 AWS 开发工具包,因为据我所知,它还没有涵盖 oauth 应用程序集成和使用外部联合身份提供商登录(如果我错了,请纠正我)。我从 AWS 托管的登录屏幕登录,当我调用他们的授权 endpoint 时,我被重定向到该屏幕.他们使用“代码”将我重定向回我的页面,我使用他们的 token endpoint 将其发回给他们获得 token 。所有这些都是教科书 oauth 2.0 的东西。

问题是,当我使用 JavaScript 浏览器重定向(window.location.href = ....)调用注销端点时,它不会清除我登录时设置的 cookie(“XSRF-TOKEN”和“cognito” ") 并且我无法手动清除它们,因为它们是从 AWS 域设置的,该域与我的站点所在的域不同。当我在地址栏中输入注销链接时,cookie 会被清除。在代码中使用 window.location.href 和在我的地址栏中放置链接之间显然存在差异。

最佳答案

要清除 sessoin,您需要使用 clearCachecId()然后重置 Cognito Id 凭据。这是我使用 AWS 开发工具包的功能:

import AWS from 'aws-sdk/global';

const getCurrentUser = () => {
const userPool = newCognitoUserPool({
UserPoolId: YOUR_USER_POOL_ID,
ClientId: YOUR_APP_CLIENT_ID
});
return userPool.getCurrentUser();
}

const signOutUser = () => {
const currentUser = getCurrentUser();

if (currentUser !== null) {
curentUser.signOut();
}

if (AWS.config.credentials) {
AWS.config.credentials.clearCachedId(); // this is the clear session
AWS.config.credentials = new AWS.CognitoIdentityCredentials({}); // this is the new instance after the clear
}
}

那应该照顾。

关于javascript - 无法从 React 应用程序中销毁 AWS Cognito session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49309928/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com