Registering New User with Initial Password Using Auth0 API

Tadashi Shigeoka ·  Sat, March 13, 2021

I’ll introduce sample code for registering a new User with an auto-generated initial password using the Auth0 API.

Auth0

Background: Want to Register New User via Auth0 API

When registering a User via the Auth0 API, you need to set an initial password, so I used generate-password - npm.

Sample Code from Initial Password Generation to Auth0 API createUser

import { ManagementClient } from 'auth0';
import { generate } from 'generate-password';

const managementClient = new ManagementClient({
  domain: process.env.AUTH0_DOMAIN,
  clientId: process.env.AUTH0_CLIENT_ID,
  clientSecret: process.env.AUTH0_CLIENT_SECRET,
  scope: process.env.AUTH0_SCOPE,
});

/**
 * @see https://auth0.com/docs/connections/database/password-strength
 */
const password = generate({
  length: 8,
  numbers: true,
  lowercase: true,
  uppercase: true,
  symbols: true,
  strict: true,
});

return await managementClient.createUser({
  email,
  password,
  /**
   * @see https://community.auth0.com/t/combine-email-verification-password-reset/7893
   */
  verify_email: false,
  connection: process.env.AUTH0_DB_CONNECTION,
});

That’s all from the Gemba on introducing sample code for setting User initial passwords via Auth0 API.

Reference Information