node.js - 使用 icognito 身份池时,在 Lambda 函数中获取 context.identity 中的用户信息
问题描述
我正在使用 lambda post 身份验证来触发 appsync 突变以将用户详细信息存储在 dynamodb 中。我想使用文档中指定的用户 context.identity.cognitoIdentityId https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#aws-appsync-resolver-context-将 IAM 授权与从 Amazon Cognito 身份池提供的凭证一起使用时的 reference-identity 。但是,当我在云手表中记录上下文时,云手表中的身份对象丢失了,因此我无法将用户的详细信息存储在 dynamodb 中。
const env = require('process').env;
const fetch = require('node-fetch');
const AWS = require('aws-sdk');
AWS.config.update({
region: env.AWS_REGION,
credentials: new AWS.Credentials(env.AWS_ACCESS_KEY_ID,
env.AWS_SECRET_ACCESS_KEY, env.AWS_SESSION_TOKEN)
});
module.exports = {
postAuthSignup: (event, context, callback) => {
console.log(`Event = ${JSON.stringify(event, null, 2)}`);
console.log(`Context = ${JSON.stringify(context, null, 2)}`);
console.log(`Env = ${JSON.stringify(env, null, 2)}`);
const Signup = `mutation Signup($input: CreateSignUpInput) {
signup(input: $input) {
id
firstname
middlename
lastname
phone{
mobile
}
email
}
}`;
const details = {
input: {
id: context.identity.cognitoIdentityId,
firstname: event.request.userAttributes.name,
lastname: event.request.userAttributes.family_name,
middlename: event.request.userAttributes.middle_name,
phone: {
mobile: event.request.userAttributes.phone_number
},
email: event.request.userAttributes.email
}
};
const post_body = {
query: Signup,
operationName: 'Signup',
variables: details
};
console.log(`Posting ${JSON.stringify(post_body, null, 2)}`);
// post GraphQl mutation to AWS AppSync using signed connection
const uri = new URL(env.GRAPHQL_API);
const httpRequest = new AWS.HttpRequest(uri.href, env.REGION);
httpRequest.headers.host = uri.host;
httpRequest.headers['Content-Type'] = 'application/json';
httpRequest.method = 'POST';
httpRequest.body = JSON.stringify(post_body);
AWS.config.credentials.get(err => {
const signer = new AWS.Signers.V4(httpRequest, "appsync", true);
signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate());
const options = {
method: httpRequest.method,
body: httpRequest.body,
headers: httpRequest.headers
};
fetch(uri.href, options)
.then(res => res.json())
.then(json => {
console.log(`JSON Response = ${JSON.stringify(json, null, 2)}`);
callback(null, event);
})
.catch(err => {
console.error(`FETCH ERROR: ${JSON.stringify(err, null, 2)}`);
callback(err);
});
});
}
};
这是我的 lambda 身份验证功能。我很乐意协助从 lambda 函数中获取 context.identity。
解决方案
推荐阅读
- python - 我如何知道一个数字是否大于或小于 Python 中的字符串?
- c++ - Catch2 未定义对 Catch::StringMaker 的引用
- mysql - 从 Powershell 运行 sql 查询的问题
- javascript - 如何从对象数组填充的选择字段中获取选定对象或索引
- android - APK安装期间的服务注册
- react-native - 使用 i18next 从应用程序更改时,如何使反应本机应用程序保存语言?
- android - Android:如何获取剪贴板数据?(无法获取剪贴板管理器)
- php - Laravel 在模态窗口中加载外部 URL
- .net - 使用 pdfsharp 填写 pdf 文档时,除非单击字段,否则填写的表单不会显示值
- c++ - wstring 数据到 const uint32_t*