【发布时间】:2021-05-06 16:14:22
【问题描述】:
我正在使用 AWS Amplify 创建 Lambda 函数、REST API 和 Cognito 用户池。我想检索向端点发出请求的 Cognito 用户,以便我可以访问他们的用户属性。
我为函数选择了无服务器 Express 模板:
app.js
app.post('/do-something', async (req, res) => {
// The user pool ID is available as an environment variable.
// I want to get the user and use their user attributes here.
});
并且客户端配置根据当前用户的token设置Authorization标头:
App.js
Amplify.configure({
API: {
endpoints: [
{
name: "sampleCloudApi",
endpoint: "https://xyz.execute-api.us-east-1.amazonaws.com/Development",
custom_header: async () => {
return { Authorization: `Bearer ${(await Auth.currentSession()).getIdToken().getJwtToken()}` }
}
}
]
}
});
事件 (req.apiGateway.event) 或上下文是否包含用户信息?或者我可以以某种方式使用 Authorization 标头吗?
另外,在 Lambda 函数中进行 Cognito 调用会是什么样子?这需要使用 Admin API 吗?
谢谢!
【问题讨论】:
-
这是我从 lambda 请求中获取用户属性的方式:stackoverflow.com/questions/68918227/…
标签: amazon-web-services aws-lambda aws-api-gateway amazon-cognito aws-amplify