Migrate existing users
Adopt Nest Auth in an existing app that already has a `users` table.
You've been running for years; you have a users table with email, password hash, name, billing info, the works. You want to adopt Nest Auth without forcing every user to reset their password.
The end state
Migration steps
Step 1 — install the auth tables
Use either migration-generate or copy-paste SQL to create all 14 nest_auth_* tables. Don't drop or modify your existing users table yet.
Step 2 — copy users into nest_auth_users
If your existing password hashes are bcrypt or scrypt, you have two options:
- Re-hash on next login. Add a
password.verifyhook that detects the legacy format, verifies against the legacy algorithm, then re-hashes with argon2 and updates the row. - Force a password reset. Mark every imported user with a metadata flag like
metadata.requires_reset: trueand refuse login until they reset.
The first option is friendlier; the second is safer.
Step 3 — add the FK column to your existing table
Step 4 — drop auth columns from app_users
email and phone should already be on nest_auth_users — drop them from app_users too, unless you specifically want them duplicated for read convenience.
Step 5 — update your TypeORM entity
Step 6 — update your application code
Anywhere you currently read user.email:
Or — better — change your queries to use the auth user's id directly:
Step 7 — register the listener
For users created going forward, the user-registered listener creates the app_users row. The migration was a one-time backfill.
Ordering and downtime
Run these steps in a single deploy where possible. If you can't:
- Step 1–2 can be done while the app is running on the old code (the new tables are inert).
- Step 3 is fast — even on millions of rows the
UPDATEis quick ifusers.emailis indexed. - Step 4 is what the live app must already tolerate. Deploy the new app code (Step 5–6) before dropping the old columns, so there's never a moment when running code expects a column that's gone.
Related
- User Model.
- Database Setup.
- Hooks Reference —
password.hash/password.verifyfor legacy hash support.