Account Migration - SuperTokens Docs

Account Migration

Migrate your users from a legacy authentication provider to SuperTokens.

The following guide shows you how to move users from your current authentication solution to SuperTokens.


Overview

The process of migrating your accounts breaks down into two parts:

Creating new users on the fly

To ensure a smooth migration process, with no downtime, you need to be able to directly create new users from the legacy sign up flow. This is necessary since there is a time gap between when you export all your data for bulk import and when you go live with SuperTokens.

New users might get created in that interval through your legacy authentication provider. Hence, you also need to create them in SuperTokens to keep the data in sync.

Adding most of your users through a bulk import

After you have set in place the lazy migration process you can move on to adding most of your users. This happens through the bulk import API. The process is asynchronous and can work with large amounts of data.

Before you start

This guide assumes that you have already integrated SuperTokens with your existing stack. If you have not, please check the Quickstart Guide and explore all the supported authentication methods.

Bulk import requires Core 10.0.0 or later and persistent database storage; the in-memory database does not support these APIs. Before importing:

For email/password users, provide either a supported passwordHash with its hashingAlgorithm, or a plainTextPassword, as defined by the bulk-import request schema. Prefer compatible bcrypt, Argon2, or Firebase scrypt hashes over plain-text passwords. Treat exports, password hashes, MFA secrets, API keys, and access tokens as credentials: encrypt them in transit and at rest, restrict access, never put them in logs or user metadata, and securely delete temporary copies after reconciliation.

Steps

1. Update the legacy sign up flow

Modify the legacy sign up flow logic to also create new users in SuperTokens. You can do this through the Import User endpoint that allows you to directly create accounts. Call the endpoint from the authentication flow used by your legacy provider.

curl -X POST "<CORE_API_ENDPOINT>/appid-public/bulk-import/import" \
  -H "api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "externalUserId": "user_12345",
  "userMetadata": {
    "firstName": "Jane",
    "lastName": "Doe",
    "department": "Engineering"
  },
  "userRoles": [
    {
      "role": "admin",
      "tenantIds": [
        "public"
      ]
    }
  ],
  "totpDevices": [
    {
      "secretKey": "JBSWY3DPEHPK3PXP",
      "period": 30,
      "skew": 1,
      "deviceName": "Main Device"
    }
  ],
  "loginMethods": [
    {
      "isVerified": true,
      "isPrimary": true,
      "timeJoinedInMSSinceEpoch": 1672531199000,
      "recipeId": "emailpassword",
      "email": "jane.doe@example.com",
      "passwordHash": "$2b$10$Tix3Vpu93kiaZRLPPzD6QOIm62x0l5gRdvlyark5S.MLn/NY6t4gS",
      "hashingAlgorithm": "bcrypt"
    }
  ]
}'

2. Export the accounts from your legacy provider

Export the users from your legacy authentication provider and adjust the data to match the request body schema used in the Add Users for Bulk Import endpoint.

3. Perform the bulk migration process

3.1 Add the accounts to import

Using the data that you have generated in the previous step, call the Add Users for Bulk Import endpoint. This step stages the data that the background job imports later. Keep in mind that the endpoint has a limit of 10000 users per request.

curl -X POST "<CORE_API_ENDPOINT>/appid-public/bulk-import/users" \
  -H "api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "users": [
    {
      "externalUserId": "user_2",
      "userMetadata": {
        "firstName": "John",
        "lastName": "Doe",
        "department": "Marketing"
      },
      "userRoles": [
        {
          "role": "editor",
          "tenantIds": [
            "public"
          ]
        }
      ],
      "loginMethods": [
        {
          "isVerified": true,
          "isPrimary": true,
          "timeJoinedInMSSinceEpoch": 1672617599000,
          "recipeId": "thirdparty",
          "email": "john.doe@gmail.com",
          "thirdPartyId": "google",
          "thirdPartyUserId": "google_987654321"
        }
      ]
    }
  ]
}'

3.2 Monitor the progress of the job

To determine if the import flow has processed all the users, call the Count Staged Users API.

3.3 Handle staged users that failed to import

Go through this step only if you have staged users that failed to import. This can happen for a number of reasons. Some common ones:

The response includes the import error messages for each specific user. Use them to determine what you need to correct in your import data.