我正在尝试在AWS Lambda函数中使用AWS Cognito用户池.我在教程中看到你需要在代码中包含amazon-cognito-identity.min.js,但我不知道如何在节点js中执行此操作.我使用npm install作为外部模块,但我不认为aws-cognito-identity作为模块存在.
我安装了aws-sdk但SDK中不存在AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool功能.
顺便说一句,这是我在Lambda中的代码:
'use strict';
var AWS= require('aws-sdk');
AWS.config.region = 'ap-northeast-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'ap-northeast-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' // your identity pool id here
});
// Need to provide placeholder keys unless unauthorised user access is enabled for user pool
//AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: 'anything'})
var poolData = {
UserPoolId : 'us-east-1_xxxxxxxxx',
ClientId : 'xxxxxxxxxxxxxxxxxxxxxxxxx'
};
var userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
module.exports.handler = function(event, context, cb) {
var attributeList = [];
var email = event.email;
var username=event.username;
var password = event.password;
var dataEmail = {
Name : 'email',
Value : email
};
var dataPhoneNumber = {
Name : 'phone_number',
Value : '+15555555555'
};
var attributeEmail = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);
userPool.signUp(username, password, attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
username = result.user;
}
); return cb(null, username);
};
这是我在测试Lambda函数时得到的错误消息:
{
"errorMessage": "AWS.CognitoIdentityServiceProvider.CognitoUserPool is not a function",
"errorType": "TypeError",
"stackTrace": [
"Module._compile (module.js:409:26)",
"Object.Module._extensions..js (module.js:416:10)",
"Module.load (module.js:343:32)",
"Function.Module._load (module.js:300:12)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)"
]
}
最佳答案 我也有同样的问题,我刚刚发现这个令人难以置信的回购:
https://github.com/kndt84/amazon-cognito-identity-js
和相关的npm包:
https://www.npmjs.com/package/amazon-cognito-identity-js-node
有用.现在(2017年7月)的好解决方案,等待Cognito的通用版本发布,这将在AWS中修复(有些人自2016年10月以来一直面临这个问题).
npm install amazon-cognito-identity-js-node
和
var AWS = require('aws-sdk');
var CognitoSDK = require('amazon-cognito-identity-js-node');
AWS.CognitoIdentityServiceProvider.AuthenticationDetails = CognitoSDK.AuthenticationDetails;
AWS.CognitoIdentityServiceProvider.CognitoUserPool = CognitoSDK.CognitoUserPool;
AWS.CognitoIdentityServiceProvider.CognitoUser = CognitoSDK.CognitoUser;
您也可以对CognitoUserAttribute等其他项目执行相同的操作.
请注意,您必须更改
const attributeEmail = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
至:
const attributeEmail = new AWS.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail.Name, dataEmail.Value);