I am trying to use cognito and register a user. I have set up my pool and app and I got the poolId and client id. have the following code:
var AWS = require('aws-sdk'); // Tried 'aws-sdk/dist/aws-sdk' too
var AWSCognito = require('amazon-cognito-identity-js'); // Tried var cognito = ... too
var CognitoUserPool = AWSCognito.CognitoUserPool;
exports.handler = function(event, context, callback) {
registerUset();
callback(null, "some success message");
}
var registerUset= function(){
var poolData = {
UserPoolId : 'xxxxxxxxxxxx', // Your user pool id here
ClientId : 'xxxxxxx' // Your client id here
}; console.log("YEEEEEEEEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var attributeList = [];
var dataEmail = {
Name : 'email',
Value : '[email protected]'
};
var dataPhoneNumber = {
Name : 'phone_number',
Value : '55555555'
};
var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);
userPool.signUp('username', 'password', attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
console.log(cognitoUser);
});
}
when I upload and run the above code in lambda test console, I get the following error:
START RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07 Version: $LATEST
2017-06-07T15:37:50.315Z 40f2d39f-4b97-11e7-a9c0-ef84b50cec07
YEEEEEEEEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSSSSS
2017-06-07T15:37:50.414Z 40f2d39f-4b97-11e7-a9c0-ef84b50cec07 TypeError:
Cannot read property 'CognitoUserPool' of undefined
at registerUset (/var/task/index.js:24:65)
at exports.handler (/var/task/index.js:8:5)
END RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07
REPORT RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07 Duration: 3621.75 ms
Billed Duration: 3700 ms Memory Size: 128 MB Max Memory Used: 31 MB
RequestId: 40f2d39f-4b97-11e7-a9c0-ef84b50cec07 Process exited before
completing request
which says that Cannot read property 'CognitoUserPool' of undefined. so means CognitoIdentityServiceProvider is undefined which is ver strange. As far as I see, I am not missing anything and also I followed the following link for code example and set up:https://github.com/aws/amazon-cognito-identity-js
for installation and packages I used
> > npm install --save amazon-cognito-identity-js
and also I installed aws-sdk. Not sure why I am getting the above error. Can anyone help?
Update:
I added the following at the begining of my code :
AWSCognito.config.region = 'us-east-1';
and I get
TypeError: Cannot set property 'region' of undefined
so I think AWSCognito is undefined.
Update: I got a suggestion to use webpack to transform my code to make it compatible for older version of javascript and also use import rather tha require. So here is my webpack.config.js
var path = require("path");
var DIST_DIR = path.resolve(__dirname, "dist");
module.exports = {
// Example setup for your project:
// The entry module that requires or imports the rest of your project.
// Must start with `./`!
entry: './',
// Place output files in `./dist/my-app.js`
output: {
path: DIST_DIR,
filename: 'index.js'
},
module: {
noParse: [
/aws\-sdk/,
],
loaders: [
{
test: /\.json$/,
loader: 'json'
}
]
}
};
After tranforming my code and running it I get the following:
{
"errorMessage": "Cannot find module './lib/browser_loader'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:417:25)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)",
"Object.<anonymous> (/var/task/index.js:3651:1)",
"__webpack_require__ (/var/task/index.js:20:30)",
"webpackUniversalModuleDefinition (/var/task/index.js:149:28)",
"Object.<anonymous> (/var/task/index.js:156:3)",
"__webpack_require__ (/var/task/index.js:20:30)",
"Object.<anonymous> (/var/task/index.js:75:18)"
]
}
Any idea?