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

标签 javascript reactjs amazon-cognito aws-cognito window.location

我正在尝试通过调用他们的注销 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/

相关文章:

javascript - 如何创建 Konva-React 上下文菜单

javascript - 使用这个(http-proxy-middleware)后,它在reactjs中给了我这个错误

amazon-web-services - 如何调试 Cognito 的 idpresponse 端点

python - 注销时使 AWS Cognito token 无效

javascript - 如何在 Wicket FeedbackPanel 的 info()/error() 方法上调用 Javascript 方法?

javascript - D3.js 圆环图中的鼠标悬停文本

javascript - FullCalendar jQuery 插件 - 设置今天的日期

javascript - 如何通过 react 拖放内容?

javascript - 在 React js 中集成 Stripe 支付

amazon-web-services - Cognito 在通过电话确认之前更改电话号码