无法从 React 应用程序中销毁 AWS Cognito 会话

Sco*_*man 6 javascript window.location reactjs amazon-cognito

我尝试通过调用注销端点来注销使用 AWS Cognito 的应用程序。我没有使用 AWS SDK,因为据我所知,它尚未涵盖 oauth 应用程序集成和使用外部联合身份提供商登录(如果我错了,请纠正我)。我从 AWS 托管的登录屏幕登录,当我调用其授权端点时,我会重定向到该屏幕。他们使用“代码”将我重定向回我的页面,我使用其令牌端点将其发回给他们以获取令牌。所有这些都是教科书上的 oauth 2.0 内容。

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

Ami*_*000 1

要清除您需要使用的会话clearCachecId(),然后重置 Cognito Id 凭据。这是我使用 AWS SDK 的函数:

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
  }      
}
Run Code Online (Sandbox Code Playgroud)

那应该解决这个问题。