【问题标题】:Cognito AmazonWebService authentication issueCognito AmazonWebService 身份验证问题
【发布时间】:2017-10-08 03:41:39
【问题描述】:

我正在使用 AmazonWebService cognito 进行用户管理,但在对我的用户池进行身份验证时遇到了一些困难。

如果我只是登录,我是否登录:

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;
        },

因为我使用这种登录方式后无法获取我的用户属性。 像这样:

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

我必须做什么?

【问题讨论】:

    标签: javascript angularjs amazon-web-services amazon-cognito aws-cognito


    【解决方案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);
                }
    
            });
        }
    

    【讨论】:

    • 感谢您的回答@Chris,如果 getSession 返回 true 是否意味着我已通过身份验证?我认为我的问题来自我的身份验证。我是否使用上面显示的登录功能进行身份验证?
    • 我添加了我使用的登录功能。从这里获得所有设置后,您就登录了。
    • 感谢@Chris 的回答,您能告诉我 _IDENTITY_POOL_ID 和 POOLID 有什么区别吗?
    • 身份池 id 是来自 Federated Identities 的 id,而池 id 来自用户池。
    • 转到您的 aws 控制台。然后是 Cognito,然后管理联合身份。
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2018-01-29
    • 2016-06-25
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2016-12-22
    相关资源
    最近更新 更多