BA
Better Auth•2mo ago
Jimmy

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:
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?
Jump to solution
10 Replies
Ping
Ping•2mo ago
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 purpose
When you add a new row to your users table, you can set the emailVerified field to false.
Jimmy
JimmyOP•2mo ago
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:
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
Ping
Ping•2mo ago
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.
Jimmy
JimmyOP•2mo ago
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.
Jimmy
JimmyOP•2mo ago
Coming back to the original question. Now I'm getting an "UNAUTHORIZED" error from better-auth
Jimmy
JimmyOP•2mo ago
No description
Jimmy
JimmyOP•2mo ago
Is it because it's not loading my .env file which has my BETTER_AUTH_SECRET?
Ping
Ping•2mo ago
It's possible. It's not very descriptive with the error message, so I can't really search through the code-base.
Solution
Jimmy
Jimmy•2mo ago
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?
Jimmy
JimmyOP•2mo ago
Email & Password | Better Auth
Implementing email and password authentication with Better Auth.

Did you find this page helpful?