Skip to Content
Auth for your appMigrating to Foir

Migrating to Foir

Moving an existing user base to Foir is one command: export your users from the provider you are leaving, then hand the file to foir users import. People whose passwords were stored as bcrypt hashes keep them and sign in with the password they already know. Everybody else sets a password the first time they sign in.

The file

A CSV with a header row, or a JSON array of objects, with one person per row or entry:

FieldRequiredWhat it holds
emailyesThe address the person signs in with.
first_namenoTheir first name.
last_namenoTheir last name.
password_hashnoThe bcrypt hash their old provider stored: $2a$, $2b$ or $2y$, cost 4 to 31.

firstName, lastName and passwordHash work too. Any other column or key stops the import before anything is sent, so a misspelt pasword_hash can’t quietly bring everybody in without a password. Remove the columns your export has that Foir doesn’t read.

email,first_name,last_name,password_hash ana@example.com,Ana,Byrne,$2b$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy ben@example.com,Ben,,
[ { "email": "ana@example.com", "first_name": "Ana", "password_hash": "$2b$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy" }, { "email": "ben@example.com" } ]

Run it

Check the file first. A dry run reads every person and says what an import would do, and changes nothing:

foir users import users.csv --dry-run foir users import users.csv

Each person gets one of three answers:

  • created: a new user in the project, holding the hash they had if the row gave one.
  • already held the address: somebody in the project already signs in with that email. They are left exactly as they are, password included, so running the same file twice is safe.
  • refused: with the reason, for example a hash that isn’t bcrypt. Fix the row and run the file again; the people already created come back as “already held the address”.

The CLI sends a large file in batches of 1,000. Add --json for every person’s answer, with the new user’s id, to feed into your own records.

The import emails nobody. People find out from you, and sign in when they next visit your app.

Importing needs users:create and users:setup: a password hash is a way into somebody’s account, so bringing one in is charged like choosing a person’s first password.

Passwords that aren’t bcrypt

Leave password_hash empty for anybody whose old provider hashed with something else (scrypt, argon2, PBKDF2). They are created with no password. When they first sign in they choose one with “Forgot password?”, or use a one-time code where the project has codes on. A new project has passwords on and one-time codes off.

This is also the right choice when you’d rather everybody picked a fresh password: import the file with no password_hash column at all.

From Clerk

Clerk’s user export is a CSV that includes each user’s bcrypt password hash.

  1. Export your users from the Clerk dashboard.
  2. Keep the email, first name, last name and password hash columns, named as in the file, and remove the rest.
  3. foir users import clerk-users.csv --dry-run, then without --dry-run.
  4. Repoint your app. Clerk’s <SignIn /> becomes hosted sign-in, and where you read Clerk’s session you verify the Foir user access token against the JWKS.

From Better Auth

Better Auth keeps users in your own database, so there is no export step: query the user table for each email and name.

  1. Write the rows to a CSV with email, first_name and last_name.
  2. Better Auth hashes passwords with scrypt by default, which isn’t bcrypt, so leave password_hash out. People who signed in with Google or another provider have no password to bring; they sign in with the same provider through Foir.
  3. foir users import better-auth-users.csv.
  4. Replace Better Auth’s client and server helpers with hosted sign-in for the sign-in page, and the user access token where you read the Better Auth session.

After you migrate

  • Wire lifecycle events. Define an operation that receives the event, then attach it as a hook on USER_CREATED, USER_UPDATED and USER_DELETED, so your systems stay in sync. See Webhooks and Hook Events. An import raises USER_CREATED for each person it creates.
  • Choose a session shape. Bearer tokens, or an HttpOnly cookie via a first-party session.
  • Watch usage. Imported users count toward MAU once they sign in.
Last updated on