gpt4 book ai didi

node.js - UserPool.getCurrentUser() 返回 null [后端]

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:07 31 4
gpt4 key购买 nike

我正在尝试设置联合身份以获得身份凭据。但是当我尝试执行 getCurrentUser() 时,得到的响应为空。另一件事是,我在后端尝试这个。那么它在后台可以工作吗?为什么在尝试 getCurrentUser 时得到空响应?有什么想法吗?

var data = {
UserPoolId: userPoolId,
ClientId: appClientId,
};

var userPool = new AmazonCognitoIdentity.CognitoUserPool(data);
console.log(userPool);
var cognitoUser = userPool.getCurrentUser();
console.log(cognitoUser);

userPool的日志响应是

CognitoUserPool {
userPoolId: 'us-east-6_hxxxx2U',
clientId: '`6heh4h8h848h4884h05',
client:
Client {
endpoint: 'https://cognito-idp.us-east-1.amazonaws.com/',
userAgent: 'aws-amplify/0.1.x js' },
advancedSecurityDataCollectionFlag: true,
storage:
{ [Function: MemoryStorage]
setItem: [Function: setItem],
getItem: [Function: getItem],
removeItem: [Function: removeItem],
clear: [Function: clear] } }

cognitoUser 的日志响应为 NULL

那么为什么响应为空,而我给出了正确的值作为输入?

最佳答案

有几个潜在的原因:

  1. 使用 getCurrentUser() 而不是 getCurrentUser(Data)
  2. 如果您没有在后台登录用户,则无法获取当前用户。如果用户在前端登录,可以使用一个函数将用户的id_token发送到后端,并在后端使用它来登录使用。

关于第二点:

id_token 包含一个称为有效负载的部分,其中包含用户的用户名和其他属性。详情:https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html#amazon-cognito-user-pools-using-the-id-token

当您使用 id_token 时,您应该在允许用户执行进一步操作之前验证签名。验证代码可以在https://github.com/awslabs/aws-support-tools/tree/master/Cognito/decode-verify-jwt中找到您可以在此处添加操作代码:

                    .....
// and the Audience (use claims.client_id if verifying an access token)

if (claims.aud != app_client_id) {

callback('Token was not issued for this audience');

}

//add your code here

callback(null, claims);

}).

catch(function() {

callback('Signature verification failed');

});

用户信息应位于声明中。

关于node.js - UserPool.getCurrentUser() 返回 null [后端],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54049865/

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