0

私はAmazonWebServiceのCognitoでユーザー管理を行っています。私は自分のユーザープールに私を認証するのにいくつかの困難を抱えています。Cognito AmazonWebService認証の問題

login: function(username, password, _poolData) { 
      var deferred = $q.defer(); 
      var authenticationData = { 
     Username : username, 
     Password : password, 
     }; 
      var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(_poolData); 
      var userData = { 
      Username : username, 
      Pool : userPool 
      }; 
      cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
      var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
      cognitoUser.authenticateUser(authenticationDetails, { 
      onSuccess: function (result) { 
        console.log('access token + ' + result.getAccessToken().getJwtToken()); 
        console.log('idToken + ' + result.idToken.jwtToken) 
        deferred.resolve('successfully logged in.'); 
       }, 
       onFailure: function(err) { 
        console.log(error); 
        alert(err); 
        deferred.reject('login failled.'); 
       }, 
      }); 
      return deferred.promise; 
     }, 

私はこのログイン方法を使用した後、私のユーザー属性を取得することはできませんので:

Doが、私はちょうど行う場合、私はログインしています。このよう :

getCognitoUserAttr: function(username, _poolData) { 
      var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(_poolData); 
      var userData = { 
      Username : username, 
      Pool : userPool 
      }; 
      cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
      cognitoUser.getUserAttributes(function(err, result) { 
       if (err) { 
         alert(err); 
         return; 
       } 
       for (var i = 0; i < result.length; i++) { 
         console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue()); 
       } 
      }); 
     } 

私は常にエラーメッセージがあります。私がしなければならない何https://docs.aws.amazon.com/fr_fr/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html

:loginメソッドから提示されたことを

Error: User is not authenticated

注意を!

答えて

1

ここに行きます。私はgetUserAttributes()という関数を作り、isAuthenticatedと同じコードを使いました。

getUserAttributes(){ 
    let cognitoUser = this.getCurrentUser(); 

    if (cognitoUser != null) { 
     cognitoUser.getSession(function (err, session) { 

      cognitoUser.getUserAttributes(function(err, result) { 
       if (err) { 
        console.log(err); 
        return; 
       } 
       for (let i = 0; i < result.length; i++) { 
        console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue()); 
       } 
      }); 

     }); 
    } 

} 

これは、私はあなたの答え@クリスのために

authenticate(username: string, password: string, callback: CognitoCallback) { 

     let authenticationData = { 
      Username : username, 
      Password : password, 
     }; 

     let authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 

     let poolData = { 
      UserPoolId : CognitoUtil._USER_POOL_ID, 
      ClientId : CognitoUtil._CLIENT_ID 
     }; 

     let userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 

     let userData = { 
      Username : username, 
      Pool : userPool 
     }; 

     let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 

     cognitoUser.authenticateUser(authenticationDetails, { 
      onSuccess: function (result) { 

       AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
        IdentityPoolId : CognitoUtil._IDENTITY_POOL_ID, 
        Logins : { 
         'cognito-idp.REGION.amazonaws.com/POOLID':result.getIdToken().getJwtToken() 
        } 
       }); 

       callback.cognitoCallback('loginSuccess', null); 

      }, 

      onFailure: function (err) { 
       callback.cognitoCallback(err.message, null); 
      } 

     }); 
    } 
+0

感謝を使用したログイン機能であるのgetSessionは私が認証されています真の平均値を返す場合のでしょうか?私の問題は私の認証によるものだと思う。上記のログイン機能で認証されていますか? – rastafalow

+1

私は私が使用するログイン機能を追加しました。 – Chris

+0

@Chrisありがとうございました。_IDENTITY_POOL_IDとPOOLIDの違いは何ですか? – rastafalow