Help with Seeding Existing Users into Better Auth
Hi everyone,
I’m looking for guidance on integrating my existing users with Better Auth. My users are already formatted in JSON, all using email/password authentication, and their passwords are already encrypted. I’ve already set up Better Auth’s options to work correctly with BCryptJS.
My setup:
Database: Neon PostgreSQL
ORM: Drizzle
Framework: Next.js 15
My concerns:
User IDs – Better Auth seems to generate its own IDs rather than using the database’s auto-incremented IDs.
Accounts Table – Since my users log in via email/password, I believe I need to create an Account record for each user.
I’m unsure how to properly seed my existing users so that Better Auth recognizes them. If anyone has experience with this or can point me in the right direction, I’d really appreciate the help!
Thanks for your time!
24 Replies
It's not ideal but what I did was this, and I just made sure my local server was running when doing so:
It would be super nice if there were a better auth server instance that you don't have to authenticate with
Wouldn't that end up re-encrypting the already encrypted passwords, rendering them unusable?! 😦
Encypted or Hashed?
encyption can be reversed
hashing cant be.
Hashed ofcourse it are passwords
Just asking for clarity. Just migrate the database by mapping your columns to the betterauth columns and then Setup better auth custom hash and verify functions to work with your current verify and hashing
Email & Password | Better Auth
Implementing email and password authentication with Better Auth.
No worries and thanks!
I missed your concerns sorry!
UserIds - https://www.better-auth.com/docs/concepts/database#id-generation
Accounts - if you currently have a single table like you will need to create both a user record in your db with the better auth fields and create a related account field with the password User Schema, Account Schema
You would want to work with the database directly in the migration process and not use the BetterAuth API to ensure existing data is migrated properly use SQL directly or Drizzle ORM in a script and highly recommend using Transactions where possible.
Database | Better Auth
Learn how to use a database with Better Auth.
@DragonCoder99
I tried using the additional fields, but it wants be to included all those fields every time every time; otherwise, I get a type error.
1. If your getting errors on the authclient read up about inferring addtional fields (Read the whole typescript page, is useful) if your have numbered ID's Interger than convert to string before migrating.
TypeScript | Better Auth
Better Auth TypeScript integration.
2. If the fields should not be updated by the user use input:false as in documentation and you dont have to add your extra fields to the adtionalfields column you could instead use hooks https://www.better-auth.com/docs/concepts/database#database-hooks to fill in those extra fields on signup like adminNotes you could leave it out and add a hook before hook that adds the adminNotes field to be like "User Signed up",
If you need to update the adminNotes field use your ORM directly.
3. I dont recommend adding oldId as means more complexity use the single userId filed and migrate your current ids to strings if they are Ints and use the advanded option to either let the db generate Ids or add a custom id generate function
Database | Better Auth
Learn how to use a database with Better Auth.
That didn't help I still get errors on my "body" in the better-auth functions.
Are you using signUpEmail to migrate? you shouldn't instead use SQL directly or ORM and use db transactions
yeah kinda what the first answer told me to do 😦
You cant since signUpEmail takes the plaintext password which since you have hashed passwords you can do it.
yeah will try to do it with Drizzle it self
Unless you have plaintext password then you can. I think the confussion can when you said seeding as this usally referrs to mockdata. You sound like you need to migrate user data
yep indeed I need to migrate and only got the hashed version of the passwords
In that case use SQL or Drizzle and write a script to do it for you
I'm on it thanks for all your help @lonelyplanet !!!
Your welcome again couldnt stress it enough https://orm.drizzle.team/docs/transactions, I promise you can save your self from failed half migrations
If it helps at all, my seeding package is open source which you can take inspiration:
https://www.better-auth-kit.com/docs/cli/seed
You guys have a different usage of seeding existing users, (and it's still do-able with my package), but it's primarily made for random data seeding.