Migrating · Migration steps
Moving your users
Bring your accounts over without moving a password. Each person reclaims every owned row at first sign-in.
- API
- —
- In the schema
ext::auth::Identity
What you’ll build#
Your old system’s accounts become people in your project. Each person reclaims every owned row at first sign-in. Nobody is emailed on the day you switch, and nobody sees another person’s rows during the move.
Import Person rows ahead of sign-in
Accounts and owned rows move into Person records carrying confirmed email addresses and upstream OAuth or OIDC subjects, without importing old password hashes.
Claim identity at first PKCE sign-in
When a user returns via OIDC, Google, GitHub, a magic link or a verified new password, claimPerson links their ext::auth::Identity once and unlocks every owned row.
The move works the same from any source and brings each person as a row with the identifiers the old system held. When that person signs in to your project, one query links the sign-in to the person’s row. Every rule in your schema reads that link.
Sort the old account data#
| From the old system | Moves? | What to do |
|---|---|---|
| Each account and the account’s rows | Yes | What to doA Person row per account. The old id is kept until the move is over. |
| An email address | Yes | What to doStored on the person. Anyone who confirms the address in your project is that person. |
| A Google or GitHub account | Yes | What to doStored as the provider’s own id for the account. The id is the same in every app. |
| An account at your identity provider | Yes | What to doKeep the provider. The project signs people in through the same provider, so people notice no change. |
| A password | No | What to doA project cannot take another system’s password hashes. People sign in with a magic link or set a new password. |
| A phone number alone | No | What to doSign-in has no text messages. Ask for an address before you move. |
Pick sign-in methods#
Turn on a sign-in method for every kind of account you have. Most apps need two methods: the provider your people already use and magic links for everyone who had a password.
# People who used Google or GitHub: the same accounts, signed in to an OAuth client of yours.napalm sign-in provider notes google --client-id <client-id> # People who came through an identity provider of yours, such as Auth0, Okta or Clerk: keep it.napalm sign-in provider notes oidc --issuer-url https://login.example.com --display-name "Example login" --client-id <client-id> # Everyone with an address: a code by email. It needs the project's mail server set (napalm sign-in email).napalm sign-in provider notes magicLink- The same provider
- Google and GitHub each give one id per account to every app that asks. The move carries the id the old system stored, so the project matches the person at the first sign-in.
- Your own provider
- An identity provider that speaks OpenID Connect stays in place. The move carries the provider’s issuer and each person’s id at the provider. The provider is one SSO connection at $30 a month.
- A magic link
- The person enters an email address and the emailed code. The code confirms the address, and the address is on the person’s row.
- A new password
- Signing up again with the same address also works once the address is confirmed. Keep
--require-verificationon, because an unconfirmed address claims nothing.
Sign-in for your app has the page and the calls for each sign-in method.
Keep a person apart from the sign-in#
A new app often links rows straight to ext::auth::Identity. A moved app cannot, because the rows arrive before anyone has signed in. Rows belong to a Person, and a person’s identity is filled in later.
type Person { # Who this is here, once they've signed in. Empty until then. identity: ext::auth::Identity { constraint exclusive; }; # Lowercased. Empty for someone the old system knew only by a provider's account. email: str { constraint exclusive; }; required name: str; # The old system's id for this row, and the sign-ins it knew this person by. Both go when the move is over. old_id: str { constraint exclusive; }; multi old_sign_ins: str; access policy self_can_read_and_change allow select, update using (.identity ?= global caller); access policy unclaimed_goes_to_its_person allow select, update using ( not exists .identity and (((.email in global caller_emails) ?? false) or ((global caller_sign_in in .old_sign_ins) ?? false)) ); # A note can't be read without its owner, so the people on a note see each other. access policy seen_by_people_on_the_same_note allow select using (exists ( select (.<owner[is Note] union .<shared_with[is Note]) filter .owner.identity ?= global caller or ((global caller in .shared_with.identity) ?? false) )); access policy the_move_can_write allow all using (global role ?= 'service');}The second rule is the claim. The third rule lets the people on a note see each other. A shared note needs the third rule because only someone who may read a note’s owner can read the note. The claim reads two caller globals that the project verifies and the caller cannot spoof.
# The addresses the caller has confirmed, lowercased.global caller_emails := str_lower(( select ext::auth::EmailFactor filter .identity ?= global caller and exists .verified_at).email); # The caller's sign-in as the old system would know it: its issuer, a space, its subject.global caller_sign_in := (global caller).issuer ++ ' ' ++ (global caller).subject;caller_emails- Only confirmed addresses. Someone who signs up with another person’s email without confirming it gets an empty set.
caller_sign_in- The issuer and subject of the caller’s sign-in, such as
https:/. An identity’s issuer and subject come from the provider, never from the app./ accounts.google.com 1167… role- The move’s own access. A service key gets
servicefrom the endpoint’sserviceGlobals. Billing & payments shows the setting. Take the rule out when the move is done.
Bring people over#
People go over in batches to the move’s own statement. The statement writes by the old id, so a batch sent twice creates no duplicate people.
# One batch of people from the old system. Sent again, a batch changes nothing it already made.for row in json_array_unpack(<json>$rows)union ( insert Person { old_id := <str>row['id'], # An address that's missing or null is left empty. email := str_lower(<str>json_get(row, 'email')), name := <str>row['name'], old_sign_ins := array_unpack(<array<str>>row['sign_ins']), } unless conflict on .old_id else ( update Person set { name := <str>row['name'], old_sign_ins := array_unpack(<array<str>>row['sign_ins']), } ))Each row’s sign_ins are the provider accounts the old system held for that person, written as the issuer, a space and the id. The page for your source has the query that reads the accounts: Supabase, Firebase, Convex, Gel or Postgres. Switching over has the script that sends the rows.
| Provider | Issuer | The id |
|---|---|---|
https:/ | The idThe account’s sub, a long number. | |
| GitHub | https://github.com | The idThe account’s numeric id, not the username. |
| Your own provider | The issuer URL you gave --issuer-url | The idThe sub of that provider’s tokens. |
Claim a person at the first sign-in#
The claim is one deployed query with no variables. The rule decides which person the caller may take, so the query names no person.
# Run once after a sign-in. It links the caller to the person moved for them, if there is one they haven't taken.update Personfilter not exists .identityset { identity := global caller }# The caller's own person: nobody, until they've claimed one.select Person { id, name, email }filter .identity ?= global caller// src/claim.tsimport { call, signIn } from './napalm' // After each sign-in: takes the person moved for this caller, if there is one. It changes nothing after the first.signIn.onChange((session) => { if (session) void call('claimPerson')})- Claimed once
- Once a person has an identity,
claimPersonmatches nothing, so calling it after every sign-in is a safe no-op. - The caller’s person only
- A caller sees an unclaimed person only if the address or the sign-in belongs to the caller. An update that names someone else’s row finds nothing.
- No reassignment
- People can update their own row, and the policy refuses any update that points the row at another identity.
- A new person
- Someone the move never brought claims nothing, and
meanswers nobody. The app then makes a new person for the caller, as for any new sign-up. - Lists fill in
- A live query that was open before the claim steps when the claim commits. The person’s rows appear without a reload.
Check the claim#
Run the four checks below on a branch with a copy of your people before anyone real signs in. napalm people reach notes <person> counts the rows a signed-up person can read and change in each type.
Sign in as a moved person
meanswers the person’s name, and the person’s rows are there. Sign out and in again, and nothing changes.Sign up with a moved person’s address without confirming it
meanswers nobody, and the person is still unclaimed. Confirm the address to claim the person.Sign in as someone the move never brought
The caller claims nothing, and a read of
Personshows no unclaimed people.Sign in with a provider account the old system knew
The caller claims the person with no email sent, even when the old system had no address for the person.