我正在使用AWS Amplify在我的应用程序中进行身份验证。我使用电子邮件地址作为MFA的用户名和电话号码。但是,我还需要电话号码唯一,因此我创建了此预注册lambda触发器:
const aws = require('aws-sdk');
exports.handler = async (event, context, callback) => {
const cognito = new aws.CognitoIdentityServiceProvider();
const params = {
AttributesToGet: [],
Filter: `phone_number = "${event.request.userAttributes.phone_number}"`,
Limit: 1,
UserPoolId: event.userPoolId,
};
try {
const result = await cognito.listUsers(params).promise();
if(result.Users.length === 0) {
callback(null, event);
} else {
const error = new Error("Phone number has already been used.");
callback(error, event);
}
} catch (err) {
console.log(err);
}
};
但是,该函数返回以下错误:
validatePhoneNumber-dev无权执行:cognito-idp:资源上的ListUsers:xxx
我该如何解决?
这意味着您的功能无权在Cognito UserPool上列出用户”>
在PreSignup-cloudformation-template.json
文件上,您需要添加所需的权限:
在文件上,搜索lambdaexecutionpolicy
,然后在其中搜索PolicyDocument
。在Statement
下添加所需的权限:
"Statement": [ ... { "Sid": "Cognito", "Effect": "Allow", "Action": [ "cognito-idp:ListUsers" ], "Resource": "arn:aws:cognito-idp:us-east-1:679504623344:userpool/xxxxx" }
在运行
amplify push
的情况下推动您的放大更改>现在应该可以工作。