---
title: Migrating
description: Move users from better-auth or your own auth.
---

# Migrating

## Users and passwords

Copy users into `auth.users`. Keep the old password hashes and check them with `legacyVerify`. Each user's hash becomes argon2id at their next sign-in.

```ts
import { timingSafeEqual, scryptSync } from "node:crypto";

password({
  legacyVerify: (hash, password) => {
    // Example: "scrypt$<salt>$<hex>"
    const [, salt, hex] = hash.split("$");
    const key = scryptSync(password, salt!, 64);
    return timingSafeEqual(key, Buffer.from(hex!, "hex"));
  },
});
```

Argon2id hashes in the standard `$argon2id$…` format (for example from `Bun.password`) verify without `legacyVerify`. better-auth's default scrypt hashes (`<salt>:<key>`) and bcrypt hashes need a `legacyVerify` that checks that format.

`auth.users.id` is a uuid. When your old ids are not uuids (better-auth ids are not), let Postgres make new ones and keep the mapping:

```sql
create table old_user_ids (old_id text primary key, user_id uuid not null references auth.users(id));

with inserted as (
  insert into auth.users (email, email_verified_at, name, image, password_hash, created_at)
  select lower(u.email), case when u.email_verified then u.created_at end, u.name, u.image, a.password, u.created_at
  from old_users u left join old_accounts a on a.user_id = u.id and a.provider_id = 'credential'
  returning id, email
)
insert into old_user_ids (old_id, user_id)
select u.id, i.id from inserted i join old_users u on lower(u.email) = i.email;
```

Then update your own tables to point at the new ids.

## Google and GitHub accounts

```sql
insert into auth.accounts (provider, provider_user_id, user_id, email)
select a.provider_id, a.account_id, m.user_id, null
from old_accounts a join old_user_ids m on m.old_id = a.user_id
where a.provider_id in ('google', 'github');
```

If you skip this, users still sign in: a provider login with a verified e-mail links to the user with that e-mail.

## Sessions

Old sessions do not carry over. Users sign in once after the switch.

## From better-auth

| better-auth | @kaaarel/auth |
| --- | --- |
| `authClient.signIn.email()` in the browser | your server function calls `auth.password.signIn()` |
| `authClient.signIn.social({ provider })` | a link to `/auth/google` |
| `auth.api.getSession({ headers })` | `auth.user()` / `auth.session()` |
| `tanstackStartCookies()` plugin | `framework: tanstackStart()` |
| schema CLI | the package's own migrations |
| organization plugin | `workspaces` |
| twoFactor plugin | `twoFactor` |
| apiKey plugin | `tokens` |
