gpt4 book ai didi

node.js - 消息 AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool 不是 aws lambda 中的函数

转载 作者:搜寻专家 更新时间:2023-10-31 23:24:04 24 4
gpt4 key购买 nike

我正在尝试在我的 AWS Lambda 函数中使用 AWS Cognito 用户池。我在教程中看到您需要在代码中包含 amazon-cognito-identity.min.js,但我不确定如何在 Node js 中执行此操作。我将 npm install 用于外部模块,但我不认为 aws-cognito-identity 作为模块存在。

我安装了 aws-sdk,但 AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool 函数在 SDK 中不存在。

顺便说一下,这是我在 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)"
]
}

最佳答案

我也遇到了同样的问题,我刚刚发现了这个令人难以置信的 repo:

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);

关于node.js - 消息 AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool 不是 aws lambda 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com