AWS Cognito:配置

时间:2017-12-11 16:23:54

标签: javascript amazon-web-services authentication

如果用户登录,我正在检查用户是否具有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:配置中缺少凭据

1 个答案:

答案 0 :(得分:1)

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

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