通过Facebook进行的AWS Cognito身份验证成功,但getCurrentUser返回null

时间:2016-12-06 14:55:29

标签: amazon-cognito amazon-cognito-facebook

在浏览器中,在Facebook登录后,调用statusChangeCallback。一切都成功了。 Cognito甚至会返回Identity 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);

            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

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

});

答案 1 :(得分:0)

您似乎需要更改 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:&#39; IDENTITY_POOL_ID&#39;,而非 IdentityPoolId:&#39;&lt;%= process.env.AWS_USERPOOLGUID %&gt;&#39;,//您的身份池ID

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

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