我正在使用无服务器框架进行身份验证。我的目标是创建一个API终端节点,该终端节点(通过AWS API Gateway)触发Lambda函数,该Lambda函数创建一个新的AWS Cognito用户。端点将有一个自定义授权者来保护它。
我的Lambda函数如下。当它运行时,我收到错误消息“ NotAuthorizedException:此用户池不允许注册”。关于如何授权我的Lambda函数创建新用户的任何想法?
'use strict';
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
var CognitoUserAttribute = AmazonCognitoIdentity.CognitoUserAttribute;
module.exports.init = (event, context, callback) => {
console.log('Lambda initiated with event:',event);
// Define AWS Cognito User Pool
var poolData = {
"UserPoolId": process.env['COGNITO_USER_POOL_ID'],
"ClientId": process.env['COGNITO_APP_CLIENT_ID']
};
var userPool = new CognitoUserPool(poolData);
console.log('userPool:',userPool);
// Define User Attributes
var attributeList = [];
var dataEmail = {
"Name": "email",
"Value": "[email protected]"
};
var attributeEmail = new CognitoUserAttribute(dataEmail);
attributeList.push(attributeEmail);
console.log('attributeList:',attributeList);
// Create User via AWS Cognito
userPool.signUp('username', 'password', attributeList, null, function(err, result) {
if(err) {
console.log('err:',err);
callback(err,null);
} else {
console.log('result:',result);
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
callback(null,result);
}
});
};
当用户池仅允许管理员通过AdminCreateUser API创建用户时,将引发“ NotAuthorizedException:此用户池不允许注册”异常。启用此设置后,将无法调用SignUp API,并且会引发此错误。
如果从lambda触发器调用此函数,则可以使用AdminCreateUser API或禁用此设置,以便您的用户池允许SignUp API调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句