BA
Better Auth•2mo ago
Roncho

How to seed users?

I want to seed some users, and I have a json with user names, emails, passwords. this is going to be a backend script that I run manually. I wonder whats the best way, currently all failing. tired await authClient.admin.createUser, and that is failing, I guess because it only works if the users who runs this is an admin user. and at this stage i'm running this as a script, with no user. also tried await auth.api.createUser , and that also failed with status 'unauthorized'. any idea?
7 Replies
bekacru
bekacru•2mo ago
you can use auth.api.signUpEmail
Roncho
RonchoOP•2mo ago
thanks. getting this error: Creating user [email protected] Error: cookies was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
Roncho
RonchoOP•2mo ago
Here is my seeding script: "use server"; import prisma from "@/lib/prisma"; import users from "./data/users.json"; import { auth } from "@/lib/auth"; async function main() { for (const user of users) { console.log(Creating user ${user.email}); await auth.api.signUpEmail({ body: { email: user.email, password: user.password, name: user.name, role: user.role, }, }); } } // run it main() .then(async () => { await prisma.$disconnect(); }) .catch(async (e) => { console.error(e); await prisma.$disconnect(); process.exit(1); });
Roncho
RonchoOP•2mo ago
and the error is: Error: cookies was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
bekacru
bekacru•2mo ago
@Roncho update to beta pnpm i better-auth@beta and you should be able to call auth.api.createUser on the server without admin session
Roncho
RonchoOP•2mo ago
ok, i'll try that, thanks!
Ping
Ping•2mo ago
if you're wanting a library provided solution to seeding your DB, I am working on something if you're still interested 👀
No description

Did you find this page helpful?