# Import and export users

# Use Excel to import users

On the User Management->User List page, click the Import button in the upper right corner, and select Import via Excel:

After downloading the Excel template and modifying it, click upload or drag and drop the Excel file to complete the user import.

# Import users through code

# Step 1: Turn off frequent registration restrictions

Before importing users, please enter Settings -> Security Information, turn off "Frequent Registration Restrictions", and then turn it on after importing users.

# Step 2: Write a cryptographic function calculation (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 enter Extensions -> Custom Password Encryption Turn on the option and write for encryption and Function to verify password.

# Step 3: Export your user data

Please export your user data to JSON format, the content is an array, each element is an object, and one element corresponds to a piece of user information, for example:

[
  {
    "uid": "1",
    "nickname": "zhang",
    "account_id": "zhang",
    "mail": "test1@123.com",
    "password": "$2b$12$nCa3WDbsc3tvM57ifzjwrOAGGuNK7EPV0R17WKcW6f13NZvX97yLe",
    "phone": "13100000001"
  },
  {
    "uid": "2",
    "nickname": "wang",
    "account_id": "wang",
    "mail": "test2@123.com",
    "password": "$2b$12$HGloOlfz1HzD0v/r5m1r7OCMcx6X85eC5.At3Ckxe.Jn/u/Za/yy2",
    "phone": "13100000002"
  },
  {
    "uid": "3",
    "nickname": "zhao",
    "account_id": "zhao",
    "mail": "test3@123.com",
    "password": "$2b$12$ia1oUZZFbEUpLvuqUsKideQq9lVkf2kq9vFaTvp7dlfeCx8UlTmDu",
    "phone": "13100000003"
  }
]

# Step 4: Import user data to Authing

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 Authing = require('authing-js-sdk')
const secret = 'xxxxxxxxxxxxxxxxxxx'
const clientId = 'xxxxxxxxxxxxxxxxxxx'

// If the file is large, it is recommended to read in batch
// Please save the user information in the same directory as this file. The content of the file is an array of user data JSON, and one element is a user information object
let users = fs.readFileSync(path.resolve('users.json'), { encoding: 'utf8' })
users = JSON.parse(users)
async function main() {
  const authing = new Authing({
    userPoolId: clientId,
    secret,
  })

  for (let i = 0; i < users.length; i++) {
    let yourUser = users[i]
    try {
      // complete field alignment here
      let registerResult = await authing.register({
        /**
         * Turn on this switch, the password field will be directly written into the Authing database, and Authing will not encrypt this field again
         * If your password is not stored in plaintext, you should keep it on and write a password function calculation
         */
        keepPassword: true,
        nickname: yourUser.nickname,
        password: yourUser.password,
        email: yourUser.mail,
        phone: yourUser.phone,

        // Store raw data for use
        oauth: JSON.stringify(yourUser),
        // If there is a registration method such as WeChat, Weibo, etc., you should also fill it out. This is very binding for specific businesses.
        // The optional value of registerMethod is default:username-password social:qq social:dingtalk social:weibo oauth:github oauth:wechat oauth:wxapp
        registerMethod: yourUser.githubId
          ? 'oauth:github'
          : 'default:username-password',
      })
      if (!registerResult._id) {
        throw Error('Registration failed')
      }
    } catch (err) {
      console.log(err)
      // Write the user who failed to import into the file
      fs.writeFileSync(
        path.resolve('users_failed.json'),
        JSON.stringify(yourUser) + '\n',
        {
          flag: 'a',
        }
      )
    }
  }
}

main()

After copying, please align the fields before executing

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

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

Encounter problems? Contact Us (opens new window), Feel free to talk.

# Export users to Excel

On the User Management-User List page, click the Export button in the upper right corner, and select Export selected users or Export all users:

After that, you can check the export progress on the export history page:

Click the Download button to download the exported Excel: