问题描述
在浏览器中,Facebook登录后,将调用statusChangeCallback。一切成功。 Cognito甚至会返回一个身份ID。但是,userPool.getCurrentUser()返回null。 Cognito认为没有经过身份验证的用户。我该如何解决?谢谢。
In the browser, after Facebook Login, statusChangeCallback is called. Everything succeeds. Cognito even returns an Identity Id. However, userPool.getCurrentUser() returns null. Cognito does not think there is an authenticated user. How can I fix that? Thanks.
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);
}
});
}
}
推荐答案
外观就像您需要更改您的 AWSCognito.config.credentials b一样。
Looks like you need to change your AWSCognito.config.credentialsFrom what you have to this:
// 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:'<%= process.env.AWS_USERPOOLGUID%>',//您的身份池ID在此处
看起来像您正在尝试访问您的 USER POOL ,而不是您的 IDENTITY POOL 。
Looks like you are trying to access your USER POOL and not your IDENTITY POOL.
Facebook用户位于身份池中因为他们是来自Facebook服务器的联合用户。
Facebook users live in the Identity Pool because they are a federated user from the Facebook server.
这篇关于通过Facebook的AWS Cognito身份验证成功,但getCurrentUser返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!