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
you can use
auth.api.signUpEmail
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-contextHere 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);
});and the error is:
Error:
cookies
was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context@Roncho update to beta
pnpm i better-auth@beta
and you should be able to call auth.api.createUser
on the server without admin sessionok, i'll try that, thanks!
if you're wanting a library provided solution to seeding your DB, I am working on something if you're still interested 👀
