2017-12-11 20 views
0

ユーザーがログインすると、IoTに必要なポリシーがあるかどうかを確認しています。AWS Cognito:設定に資格情報がありません

初めてログインすると正常に動作します。

私がログアウトしていて、資格情報が欠落しているいくつかの理由で、別のユーザーでログインしてみてください、と私はその、再び作業、ページを更新しています....

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: Missing credentials in config

答えて

0

私は自分でそれを修正します。キャッシュされた資格情報をクリアする必要があります。

$('#logout').click(function() { 
    currentUser = userPool.getCurrentUser(); 
    currentUser.signOut(); 
    AWS.config.credentials.clearCachedId(); 
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({}); 
    location.reload(); 
}); 
関連する問題