GenAuth DocsDocuments
Concepts
User Guide
Development Integration
Metadata
Development Integration
Multi-tenant (beta)
Console Documentation
Multi-tenant Console
Tenant Console
Saas Application Demo
Concepts
User Guide
Development Integration
Metadata
Development Integration
Multi-tenant (beta)
Console Documentation
Multi-tenant Console
Tenant Console
Saas Application Demo
Old version
User Guide
  • Quick Start

  • Authentication

  • Access Control

  • Authorization

  • Adaptive MFA

  • User Account Management

  • User Directory Management

  • Applications

  • Become a Federation Authentication Identity Provider

  • Connect External Identity Providers (IdP)

  • WeChat Ecosystem Full Scenario Capabilities

  • Migrate Users to GenAuth

    • Import Users Using SDK
    • Custom Password Encryption Method
  • Security Settings

  • Branding

  • Automation

  • Audit Logs

  • Setting

  • FAQ

  1. User Guide
  2. /
  3. Migrate Users to GenAuth
  4. /
  5. Import Users Using SDK

¶ Import users using SDK

Update time: 2025-04-11 11:21:15
Edit

GenAuth supports SDKs for multiple languages including Java, JavaScript/Node.js, Python, PHP, C#, Go, and Ruby:

  • Java/Kotlin
  • JavaScript/Node.js
  • Python
  • PHP
  • C#
  • Go
  • Ruby

This article uses Node.js as an example to introduce how to write a script to import users. You can choose the language you are familiar with.

¶ Step 1: Use a custom password function (optional)

If the password field in your user data table is in plain text, you can skip this step; if it is in cipher text, you need to go to Basic Configuration -> Password Management -> Custom Password Encryption Method to enable the option and write a function for encrypting and verifying passwords. For details, see Custom Password Encryption Method.

¶ Step 2: Export your user data

Please export your user data to JSON format, the content is an array, each element is an object, one element corresponds to a user's information, save it as users.json, for example:

[
  {
    "uid": "GEN_AUTH_USER_1",
    "nickname": "zhang",
    "account_id": "zhang",
    "mail": "test1@123.com",
    "password": "$2b$12$nCa3WDbsc3tvM57ifzjwrOAGGuNK7EPV0R17WKcW6f13NZvX97yLe",
    "phone": "13100000001",
    "emailVerified": true,
    "loginsCount": 4
  },
  {
    "uid": "GEN_AUTH_USER_2",
    "nickname": "wang",
    "account_id": "wang",
    "mail": "test2@123.com",
    "password": "$2b$12$HGloOlfz1HzD0v/r5m1r7OCMcx6X85eC5.At3Ckxe.Jn/u/Za/yy2",
    "phone": "13100000002",
    "emailVerified": false,
    "loginsCount": 12
  },
  {
    "uid": "GEN_AUTH_USER_3",
    "nickname": "zhao",
    "account_id": "zhao",
    "mail": "test3@123.com",
    "password": "$2b$12$ia1oUZZFbEUpLvuqUsKideQq9lVkf2kq9vFaTvp7dlfeCx8UlTmDu",
    "phone": "13100000003",
    "emailVerified": true,
    "loginsCount": 0
  }
]

¶ Step 3: Confirm the mapping relationship between user fields

Before you start importing, you need to confirm the mapping relationship between your user structure and the GenAuth user field. You can get all the fields and their definitions of the GenAuth user here.

¶ Step 4: Import user data to GenAuth

If you don't have a NodeJS environment, you need to install NodeJS (opens new window) first.

Create an index.js file.

Paste the following js script into index.js:

const fs = require("fs");
const path = require("path");

const { ManagementClient } = require("authing-js-sdk");
const userPoolId = "xxxxxxxxxxxxxxxxxxx";
const secret = "xxxxxxxxxxxxxxxxxxx";

// If the file is large, it is recommended to read it in batches
// Please save the user information and this file in the same directory. The file content is an array JSON of user data. One element is an information object of one user. Here, read the users.json mentioned above
let users = fs.readFileSync(path.resolve("users.json"), { encoding: "utf8" });
users = JSON.parse(users);
async function main() {
  const managementClient = new ManagementClient({
    userPoolId,
    secret,
  });

  for (let i = 0; i < users.length; i++) {
    let yourUser = users[i];
    try {
      // Complete field alignment here
      await managementClient.users.create(
        {
          nickname: yourUser.nickname,
          password: yourUser.password,
          email: yourUser.mail,
          emailVerified: yourUser.emailVerified,
          phone: yourUser.phone,
          loginsCount: yourUser.loginsCount,
          // Store raw data for future use
          oauth: JSON.stringify(yourUser),
        },
        {
          /**
           * Turn on this switch, the password field will be written directly to the GenAuth database, and GenAuth will not encrypt this field again
           * If your password is not stored in plain text, you should keep it turned on and write a password function calculation
           */
          keepPassword: true,
        }
      );
    } catch (err) {
      console.log(err);
      // Write users who failed to import to a file
      fs.writeFileSync(
        path.resolve("users_failed.json"),
        JSON.stringify(yourUser) + "\n",
        {
          flag: "a",
        }
      );
    }
  }
}

main();

After copying, please align the fields and then execute

$ npm install authing-js-sdk
$ node index.js

The code can be viewed on GitHub: users-migration (opens new window)

¶ Get help

Have a problem? Contact us (opens new window), Feel free to talk.

Previous article: Migrate Users to GenAuth Next article: Custom Password Encryption Method
  • Step 1: Use a custom password function (optional)
  • Step 2: Export your user data
  • Step 3: Confirm the mapping relationship between user fields
  • Step 4: Import user data to GenAuth
  • Get help

User identity management

Integrated third-party login
Customized authentication process

Enterprise internal management

Single sign-on
Multi-factor authentication
Permission management

Developer

Development Documentation
GitHub (opens new window)

Company

official@genauth.ai
16th Floor, Building B, Beichen Century Center, Chaoyang District, Beijing (Total)
Room 406, 4th Floor, Block B, No. 200, Tianfu Fifth Street, High-tech Zone, Chengdu (Branch)

© Beijing Steamory Technology Co., Ltd.