gpt4 book ai didi

node.js - AWS Lambda - 如何使用 context.identity.cognitoIdentityId 获取相关的 Cognito 用户池用户数据?

转载 作者:太空宇宙 更新时间:2023-11-04 01:52:44 25 4
gpt4 key购买 nike

在我的 Web 客户端中,我直接通过以下方式调用我的 AWS Lambda 函数:

// Build the logins object for the credentials
var loginKey = "cognito-idp." + config.awsRegion + ".amazonaws.com/" + config.identity.UserPoolId;
var logins = {};
logins[loginKey] = jwtToken;

// Instantiate the credentials.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.identity.IdentityPoolId,
Logins: logins
});

// Must refresh the credentials prior to calling the function.
AWS.config.credentials.refresh(function(err) {
... // Error handling excluded

var lambda = new AWS.Lambda({region: config.awsRegion, apiVersion: '2015-03-31'});
var params = {
FunctionName: functionName,
InvocationType : 'RequestResponse',
LogType : 'None',
Payload: JSON.stringify(data)
};

lambda.invoke(params, function(err, data){
... // Data handling
});
});

在我的 Lambda 函数中,我验证了 context.identity.cognitoIdentityId 和 context.identity.cognitoIdentityPoolId 的值,如文档 here 中所述。 。以下是一个非常简化的 Lambda 函数,用于准确显示我所询问的属性:

exports.handler = (event, context, callback) => {
console.log(context.identity.cognitoIdentityId); // Has value
console.log(context.identity.cognitoIdentityPoolId); // Has value

callback(null, "success");
};

我认为通过这些信息我可以获取 Cognito 用户数据,包括利用这些信息的属性,但我还没有找到方法。 This最近的 SO 线程是我见过的最接近的,但即使他们也想出了一个“解决方法”类型的解决方案。

有人知道如何利用 Lambda 函数 context.identity 数据获取 Cognito 用户数据吗?谢谢!

最佳答案

不确定您是否解决了这个问题,但是在您的 API 网关中,在集成请求下,您必须设置您的正文映射模板,如果您使用 application/json 的“默认”模板,这会将数据传递到您的应用程序。

您将看到它创建的“上下文”对象。然后在您的 lambda 应用程序中:

Node 代码:

exports.handler = (event, context, callback) => {
let what = event.context;
let who = what['cognito-authentication-provider']; // or match what the template setup

callback(null, "cognito provider: " + who);
};

希望这有帮助!

关于node.js - AWS Lambda - 如何使用 context.identity.cognitoIdentityId 获取相关的 Cognito 用户池用户数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49058553/

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