How do I seed users for development env?
Hello 👋 -- The title basically says it. I'm using prisma as my database adapter. How do I correctly seed users for development purposes? Do I import the auth and use auth.api.createUser({...}) or should I use my prisma client to directly insert? I have email verification enabled and I obviously want to skip that for this purpose. What's the idiomatic approach for this? Thanks in advance!
Solution:Jump to solution
I think I'm going to punt and go back to using prisma to directly insert. Do you know how to correctly hash the password?
10 Replies
I recommend using the
auth.api.createUser({...})
fn, however creating users directly in your table should work just fine as well.
I have email verification enabled and I obviously want to skip that for this purposeWhen you add a new row to your users table, you can set the
emailVerified
field to false
.If I want to directly insert using prisma, is the User table the only one that gets a new record?
Is the accounts table left alone and only for social accounts?
When I import my better-auth file, I get an error from typescript complaining that "@/lib/auth.ts" isn't found. Do you know how I'd get around this if I go the better-auth route?
My seed command looks like this in my package.json:
I knew something felt off!
Good call. 😅
Yes, you need to also add a row to
accounts
table as well!
Make sure to set providerId
to credential
.
And if you do not intend to sign-into any of these accounts, then you can probably just use a fake password.
Otherwise, you'll have to look into hashing passwords yourself.
It's due to your import alias.
Most frameworks's compilers handles this for you, it converts the alias to the real path, however tsc (including ts-node) won't do this for you.
You can fix this by just providing the path manually, relatively or absolute should both work.I have other files imported in auth.ts that use that syntax too.
I was able to solve this using tsconfig-paths. https://typestrong.org/ts-node/docs/paths/
paths and baseUrl | ts-node
You can use ts-node together with tsconfig-paths to load modules according to the paths section in tsconfig.json.
Coming back to the original question. Now I'm getting an "UNAUTHORIZED" error from better-auth

Is it because it's not loading my .env file which has my BETTER_AUTH_SECRET?
It's possible.
It's not very descriptive with the error message, so I can't really search through the code-base.
Solution
I think I'm going to punt and go back to using prisma to directly insert. Do you know how to correctly hash the password?
Email & Password | Better Auth
Implementing email and password authentication with Better Auth.