Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aws cognito create user by admin nodejs

I am using javascript sdk amazon-cognito-identity-js and i am trying to create a user in cognito in nodejs but error is coming below is my code:-

var AWS = require("aws-sdk")
var params = {
    UserPoolId: "id",
    Username: req.body.username,
    DesiredDeliveryMediums: ["EMAIL"],
    ForceAliasCreation: false,
    TemporaryPassword: req.body.password,
    UserAttributes: [
      { Name: "name", Value: req.body.name },
      { Name: "email", Value: req.body.user_email}
    ],
 };
let client = new AWS.CognitoIdentityServiceProvider();
client.adminCreateUser(params, function(err, data) {
    if (err) {
        console.log("EE",err);
      //  reject(err);
    } else {
        console.log("DDD",data);
        //resolve(data);
    }
})

But i am getting this error using the code:-

EE { UnknownError: Not Found at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:48:27) }

Please Help me Out to figure out this issue.

like image 632
Aditya Avatar asked Oct 24 '25 20:10

Aditya


2 Answers

Here is the solution.

Pre-requirement If you want to use the credential in aws-sdk (Manual Process) 1. Create IAM User and Assign a Cognito role to your user. 2. Generate Access Key and Secret Key for that user. 3. Use that Access Key and Secret Key in aws-sdk.

Like This,

let AWS = require("aws-sdk");
AWS.config.update({
    accessKeyId: "YOURKEY",
    secretAccessKey: "YOURSECRET",
    region: "YOURREGION"
});

Create object of CognitoIdentityServiceProvider class

const COGNITO_CLIENT = new AWS.CognitoIdentityServiceProvider({
  apiVersion: "2016-04-19",
  region: "us-east-1"
});


var poolData = {
    UserPoolId: "XXXXXXXXX",
    Username: "[email protected]",
    DesiredDeliveryMediums: ["EMAIL"],
    TemporaryPassword: "Abc@321",
    UserAttributes: [
      {
        Name: "email",
        Value: "[email protected]"
      },
      {
        Name: "email_verified",
        Value: "true"
      }
    ]
  };
  COGNITO_CLIENT.adminCreateUser(poolData, (error, data) => {
    console.log(error);
    console.log(data);
    callback(null, {
      statusCode: 200,
      body: JSON.stringify(data)
    });
  });

Or else you can directly assign IAM Role to your EC2 Instance in that case you do not need to set credentials in AWS.config Section.

like image 126
Ashish Kadam Avatar answered Oct 26 '25 10:10

Ashish Kadam


And for AWS SDK v3 and ES6:

Install the Library

npm install @aws-sdk/client-cognito-identity-provider --save

Import the Module

import { CognitoIdentityProviderClient, AdminCreateUserCommand } from "@aws-sdk/client-cognito-identity-provider";

Create the Client

const cognitoClient = new CognitoIdentityProviderClient({ region: 'eu-west-1' });

Create the User

const command = new AdminCreateUserCommand({
      UserPoolId: USER_POOL_ID,
      Username: EMAIL_ADDRESS,
      DesiredDeliveryMediums: ['EMAIL'],
      TemporaryPassword: TEMPORARY_PASSWORD
    });

let response = await cognitoClient.send(command);

Note that you'll need to have cognito-idp:AdminCreateUser permission for this to work.

like image 41
CharlesA Avatar answered Oct 26 '25 10:10

CharlesA