Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cognito AdminCreateUser set password and confirmed

Tags:

aws-sdk

When using Cognito's AdminCreateUser how can I specify a permanent password at user creation time? Further, how can I set this user to "confirmed"?

like image 778
Mark Richman Avatar asked Sep 15 '25 21:09

Mark Richman


2 Answers

From digging a lot into the docs it's at least a 2 step process - create the user with a temporary password and 'complete' their registration by setting a permanent one. Posting a NodeJS solution.

  1. First you create the user with a temporary (not really used) password using the SDK:
import { CognitoIdentityServiceProvider } from 'aws-sdk';

const adminCreateUser = async (
  username: string,
  password: string
): Promise<void> => {
  return new Promise<void>((resolve, reject) => {
    new CognitoIdentityServiceProvider({
      region: '<Pool region>',
      accessKeyId: '<AWS user access key id>',
      secretAccessKey: '<AWS user access secret>',
    }).adminCreateUser(
      {
        Username: username,
        TemporaryPassword: password,
        UserPoolId: '<Cognito User Pool ID>',
      },
      (err, result) => {
        if (err) {
          return reject(err);
        }

        resolve();
      }
    );
  });
};
  1. As per the docs we need to call authenticateUser to trigger a newPassowrdRequired callback in which case we call completeNewPasswordChallenge with the permanent password:
import {
  AuthenticationDetails,
  CognitoUser,
  CognitoUserPool,
} from 'amazon-cognito-identity-js';

export const cognitoPool: CognitoUserPool = new CognitoUserPool({
  UserPoolId: '<Cognito Pool ID>',
  ClientId: '<Cognito App Client ID>',
});

export const adminConfirmUser = async (
  username: string,
  password: string
): Promise<void> => {
  return new Promise<void>((resolve, reject) => {
    const cognitoUser = new CognitoUser({
      Username: username,
      Pool: cognitoPool,
    });

    cognitoUser.authenticateUser(
      new AuthenticationDetails({
        Username: username,
        Password: password,
      }),
      {
        onSuccess: (session, userConfirmationNecessary) => {
          // User is already confirmed
          resolve();
        },
        onFailure: err => {
          // An error
          reject(err);
        },
        newPasswordRequired: (userAttributes, requiredAttributes) => {
          cognitoUser.completeNewPasswordChallenge(password, null, {
            onSuccess: session => {
              // User confirmed
              resolve();
            },
            onFailure: err => {
              // Error confirming user
              reject(err);
            },
          });
        },
      }
    );
  });
};

Note that you may need to pass required attributes when confirming the user registration depending on how you've setup your Cognito pool.

like image 59
Atanas Rusenov Avatar answered Sep 19 '25 15:09

Atanas Rusenov


Another option is to invoke AdminSetUserPassword command after user was created with Permanent: true parameter.

Ref: AdminSetUserPassword documentation

like image 36
Valera Avatar answered Sep 19 '25 14:09

Valera