gpt4 book ai didi

amazon-cognito - 通过 Facebook 的 AWS Cognito 身份验证成功,但 getCurrentUser 返回 null

转载 作者:行者123 更新时间:2023-12-04 04:27:29 29 4
gpt4 key购买 nike

在浏览器中,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);

}
});
}
}

最佳答案

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

});

关于amazon-cognito - 通过 Facebook 的 AWS Cognito 身份验证成功,但 getCurrentUser 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998272/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com