对于其他希望深入了解这个问题的人,下面是一个示例
下面的 lambda 函数 #1 包含两个自定义属性 ida 和 ethaddress。在 Cognito 用户池的 PreSignUpHook 期间调用 lambda
#2(在事件更改日志之前)这些属性的原始值为 ida=1 和 ethaddress=ABCD
#3(事件更改后的日志)反映了这些属性的更改值:
ida=2 和 ethaddress=EFGH
但是,保存到 cognito 的值是原始值:ida=1 和 ethaddress=ABCD。因此,在 presignuphook 期间更新 userAttributes 并不像某些答案中所建议的那样工作。
附带说明,当响应对象中的预定义属性被修改时,它们会按预期更新:
"response": {
"autoConfirmUser": true,
"autoVerifyEmail": false,
"autoVerifyPhone": false
}
1.拉姆达:
'use strict';
global.fetch = require('node-fetch')
module.exports.preSignUp = async (event, context, callback) => {
// Set the user pool autoConfirmUser flag after validating the email domain
let data = await fetch("http://***.***.***/api/members/create",
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: "POST",
})
.then(res => res.json())
.then(res => res);
event.response.autoConfirmUser = true;
console.log('before event:', JSON.stringify(event));
event.request.userAttributes['custom:ethaddress'] = String(data.address);
event.request.userAttributes['custom:ida'] = "2";
console.log('Received event:', JSON.stringify(event));
console.log('Address:', data.address);
// Return to Amazon Cognito
callback(null, event);
};
2.
事件更改日志之前:
2019-01-20T01:02:24.639Z edce636e-75ea-492b-b6a0-dd4f22dc9038 before event:
{
"version": "1",
"region": "us-east-1",
"userPoolId": "us-east-1-*****",
"userName": "*******@gmail.com",
"callerContext": {
"awsSdkVersion": "aws-sdk-unknown-unknown",
"clientId": "******************"
},
"triggerSource": "PreSignUp_SignUp",
"request": {
"userAttributes": {
"custom:ida": "1",
"custom:ethaddress": "ABCD",
"email": "*******@gmail.com"
},
"validationData": {}
},
"response": {
"autoConfirmUser": true,
"autoVerifyEmail": false,
"autoVerifyPhone": false
}
}
3.
事件更改日志之后:
Received event:
{
"version": "1",
"region": "us-east-1",
"userPoolId": "us-east-1_0BaE6eaTY",
"userName": "*******@gmail.com",
"callerContext": {
"awsSdkVersion": "aws-sdk-unknown-unknown",
"clientId": "*****************"
},
"triggerSource": "PreSignUp_SignUp",
"request": {
"userAttributes": {
"custom:ida": "2",
"custom:ethaddress": "EFGH",
"email": "*******@gmail.com"
},
"validationData": {}
},
"response": {
"autoConfirmUser": true,
"autoVerifyEmail": false,
"autoVerifyPhone": false
}
}
更新:
似乎没有办法将其作为 PRESIGNUP 流程的一部分
但是,可以在下面提供的 cognito 示例中将其作为 POSTCONFIRMATION 触发器来执行。
一些注意事项。
- 自定义属性已添加到 cognito 中并且是可变的。
- 在App客户端-->显示详情-->“设置属性读写权限”
确保自定义属性具有以下读写权限。
- 确保 lambda 函数具有允许其执行的角色:adminUpdateUserAttributes
例如。将 AmazonCognitoPowerUser 策略附加到 LambaRole。
module.exports.postConfirmation = async (event, context,callback) => {
const cognitoIdServiceProvider = new CognitoIdentityServiceProvider({
region: 'us-east-1'
});
var params = {
UserAttributes: [
{
Name: 'custom:sillyName',
Value: 'customSillyName'
}
],
UserPoolId: event.userPoolId,
Username: event.userName
}
cognitoIdServiceProvider.adminUpdateUserAttributes(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
callback(null,event);
};
请注意,如果您尝试在 preSignUp 触发器钩子中使用 cognitoIdServiceProvider.adminUpdateUserAttributes,您将收到一个异常,说明用户尚未退出