【问题标题】:AWS Cognito Authentication via Facebook succeeds but getCurrentUser returns null通过 Facebook 的 AWS Cognito 身份验证成功,但 getCurrentUser 返回 null
【发布时间】:2017-04-21 06:19:55
【问题描述】:

在浏览器中,在 Facebook 登录之后,调用 statusChangeCallback。一切顺利。 Cognito 甚至返回一个身份 ID。但是, userPool.getCurrentUser() 返回 null。 Cognito 认为没有经过身份验证的用户。我该如何解决?谢谢。

function statusChangeCallback(response) {
    if(response.status == 'connected' && response.authResponse) {
        testAPI()

        console.log("FB statusChangeCallback", JSON.stringify(response))

        AWSCognito.config.credentials = new AWSCognito.CognitoIdentityCredentials({
            IdentityPoolId : '<%=process.env.AWS_USERPOOLGUID%>', // your identity pool id here
            Logins : {
                'graph.facebook.com': response.authResponse.accessToken
            }
        });
        console.log(JSON.stringify(AWSCognito.config.credentials))


        AWSCognito.config.region = '<%= process.env.AWS_REGION%>'

        AWSCognito.config.credentials.refresh(function(error) {
            if (error) {
                console.error("AWSCognito.config.credentials.get", error);
            } else {
                console.log("Cognito Identity Id", AWSCognito.config.credentials.identityId);
                console.log('Successfully logged!');
                var cognitoUser = userPool.getCurrentUser();
                console.log('cognitoUser', cognitoUser);

            }
        });
    }
}

【问题讨论】:

    标签: amazon-cognito amazon-cognito-facebook


    【解决方案1】:
    userPool.getCurrentUser();
    

    指与特定用户池相关的经过身份验证的用户。在上面的代码中,您正在做的是使用 Facebook 身份获取 AWS 凭证。但是,当前用户是指用户池中最后一个经过身份验证的用户。验证成功后,将其保存在本地存储中。所以你需要先进行身份验证,类似于下面的代码。

    var authenticationData = {
        Username : 'username',
        Password : 'password',
    };
    var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
    var poolData = { 
        UserPoolId : '...', // Your user pool id here
        ClientId : '...' // Your client id here
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
        Username : 'username',
        Pool : userPool
    };
    var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            console.log('access token + ' + result.getAccessToken().getJwtToken());
    
            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                IdentityPoolId : '...', // your identity pool id here
                Logins : {
                    // Change the key below according to the specific region your user pool is in.
                    'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>' : result.getIdToken().getJwtToken()
                }
            });
    
            // Instantiate aws sdk service objects now that the credentials have been updated.
            // example: var s3 = new AWS.S3();
    
        },
    
        onFailure: function(err) {
            alert(err);
        },
    
    });
    

    【讨论】:

    • 这并没有解释如何在单击 Facebook 登录按钮后保护经过身份验证的会话和用户,而是解释用户名 + 密码组合登录。
    【解决方案2】:

    您似乎需要更改您的 AWSCognito.config.credentials 从你所拥有的到这个:

    // Add the Facebook access token to the Cognito credentials login map.
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
      IdentityPoolId: 'IDENTITY_POOL_ID',
      Logins: {
        'graph.facebook.com': response.authResponse.accessToken
      }
    });
    
    // Obtain AWS credentials
    AWS.config.credentials.get(function(){
        // Access AWS resources here.
    });
    

    注意 : IdentityPoolId: 'IDENTITY_POOL_ID', 而不是 IdentityPoolId : '', // 你的身份池id在这里

    您似乎正在尝试访问您的USER POOL,而不是您的IDENTITY POOL

    Facebook 用户存在于身份池中,因为他们是来自 Facebook 服务器的联合用户。

    【讨论】:

    • 另外,AWS.config.credentials.get 类似于 AWS.config.credentials.refresh
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2016-12-11
    • 2018-01-29
    • 2016-06-25
    • 1970-01-01
    • 2018-08-31
    • 2017-06-30
    • 2017-01-02
    相关资源
    最近更新 更多