Node.js getCurrentUser()返回null[后端]

Node.js getCurrentUser()返回null[后端],node.js,amazon-cognito,Node.js,Amazon Cognito,我正在尝试设置联合身份以获取身份的凭据。但是当我尝试执行getCurrentUser()时,得到的响应为null。关于这一点的另一件事是,我在后端尝试这一点。那么它会在后端工作吗?为什么在尝试getCurrentUser时得到空响应??有什么想法吗 var data = { UserPoolId: userPoolId, ClientId: appClientId, }; var userPool = new AmazonCognitoIdentity.CognitoUse

我正在尝试设置联合身份以获取身份的凭据。但是当我尝试执行getCurrentUser()时,得到的响应为null。关于这一点的另一件事是,我在后端尝试这一点。那么它会在后端工作吗?为什么在尝试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


那么,为什么响应为空,而我提供正确的值作为输入?

几乎没有潜在的原因:

  • 使用
    getCurrentUser()
    而不是
    getCurrentUser(数据)
  • 如果您没有在后端登录用户,则无法获取当前用户。如果用户在前端登录,您可以使用一个函数将用户的id_令牌发送到后端,并使用它在后端登录使用 关于第二点:

    id_令牌包含一个名为payload的部分,其中包含用户的用户名和其他属性。详情如下:

    使用id_令牌时,应先验证签名,然后再允许用户执行进一步操作。可在中找到用于验证的代码 您可以在此处添加操作代码:

                        .....
                        // 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');
    
                    });
    

    而且用户信息应该在
    声明中

    因为我在这里发布的代码是针对前端的。有一篇文章清楚地说明了我们应该如何验证用户


    因此,在正确的身份验证流之后,我们将使用
    cognitoidentity.getCredentialsForIdentity()
    [参考官方sdk文档]

    传递所需的数据,那么我如何在后端登录用户?您能指定使用什么功能吗?用户是否从前端登录?否。我将令牌传递给前端。就这样!您是否使用Cognito托管ui?否。我使用admin\u srp\u auth从后端登录用户。你能解释一下你在答案上写的第二点吗??