Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cognito adminCreateUser from java script SDK

I'm trying to create a user in a AWS User Pool from an AWS Lambda

I tried with this script took from what seems to be the official JavascriptSDK for the AWS but can't get it working. http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#adminCreateUser-property

I keep getting this error:ypeError: cognitoidentityserviceprovider.adminCreateUser is not a function

var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});

var params = {
    UserPoolId: 'eu-west-1_XXXXXXXX', /* required */
    Username: '[email protected]', /* required */
    DesiredDeliveryMediums: [
        'EMAIL'
    ],
    ForceAliasCreation: false,
    MessageAction: 'SUPPRESS',
    TemporaryPassword: 'tempPassword1',
    UserAttributes: [
        {
            Name: 'email', /* required */
            Value: '[email protected]'
        },
        {
            Name: 'name', /* required */
            Value: 'Me'
        },
        {
            Name: 'last_name', /* required */
            Value: 'lastme'
        }
        /* more items */
    ]
};
cognitoidentityserviceprovider.adminCreateUser(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
    callback(null, data);
});
like image 812
Rushikesh Kshirsagar Avatar asked Feb 15 '17 09:02

Rushikesh Kshirsagar


People also ask

How do I get my AWS Cognito Clientid?

The User Pool Client ID is available from the Amazon Cognito User Pools console in the App Clients section. You should create an App Client if it doesn't already exist. Make sure to uncheck the "Generate client secret" box.

How do I use AWS Cognito in node JS?

First login to your aws account and go to cognito section. If you don't have an aws account yet, create a free account which will be free for an year. Then go to Cognito section and then create a user pool. Give a name for the pool.


1 Answers

For adminCreateUser (you basically require the aws sdk, configure credentials, instantiate the client and call the specific operation).

var aws = require('aws-sdk');
aws.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});

var CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
var client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19 });
//your code goes here

Note that there might be different ways in which you can configure the AWS credentials to call the operation. You do need credentials as this is an authenticated operation. Other admin operations are similar, you just need to pass the appropriate parameters as JSON in the call.

According to this, the AWS SDK for JavaScript version 2.7.25 which contains the adminCreateUser operation should be available.

http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html

like image 155
Ionut Trestian Avatar answered Sep 27 '22 22:09

Ionut Trestian