AWS Cognito:配置中缺少凭据

问题描述 投票:1回答:1

如果用户登录,我正在检查用户是否具有IoT所需的策略,如果没有,我正在附加它。

如果我是第一次登录,这样可以正常工作。

现在,当我退出,并尝试使用其他用户登录时,由于某种原因缺少凭据,当我刷新页面时,它再次工作....

window.login = function() {
    var shadowsRegistered = false;

    AWSCognito.config.region = AWSConfiguration.region;
    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: AWSConfiguration.IdPoolId
    }); 

    var authenticationData = {
      Username : document.getElementById("benutzername").value,
      Password : document.getElementById("passwort").value
    };

    var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);

    var poolData = {
      UserPoolId : AWSConfiguration.UserPoolId, 
      ClientId :   AWSConfiguration.ClientAppId 
    };

    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

    var userData = {
      Username : document.getElementById("benutzername").value,
      Pool : userPool
    };

    var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);

    cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: function (result) {
        AWS.config.region = AWSConfiguration.region;

        var auth_params = {
          IdentityPoolId: AWSConfiguration.IdPoolId,
          Logins : {
            'cognito-idp.eu-central-1.amazonaws.com/eu-central-XXXX' : result.getIdToken().getJwtToken()
          }
        };

        AWS.config.credentials = new AWS.CognitoIdentityCredentials(auth_params); 
        var cognitoIdentity = new AWS.CognitoIdentity();        
        cognitoIdentity.getId(auth_params, function(err, data) {
          if (err) {
            cognitoId = AWS.config.credentials.identityId;
          }
          else{
            cognitoId = data.IdentityId;
          }
          var iot = new AWS.Iot();

          iot.listPrincipalPolicies({principal: cognitoId}, function(err, data) {
            if (err) {
              console.log(err, err.stack);  //ERROR on 2nd login
            }
            else{
              // not related, works on the first login..

我收到的错误:

CredentialsError:配置中缺少凭据

javascript amazon-web-services authentication
1个回答
1
投票

我自己修理了。您需要清除缓存的凭据。

$('#logout').click(function() {
  currentUser = userPool.getCurrentUser();
  currentUser.signOut(); 
  AWS.config.credentials.clearCachedId();
  AWS.config.credentials = new AWS.CognitoIdentityCredentials({});
  location.reload();
});
© www.soinside.com 2019 - 2024. All rights reserved.