Seeding an admin user
I want to seed an admin user in my Express app.
export const auth = betterAuth({
appName: 'LaundryApp',
basePath: '/api/auth',
trustedOrigins: ['laundryapp://'],
database: prismaAdapter(prisma, {
provider: 'sqlite',
}),
plugins: [admin(), expo()],
emailAndPassword: {
enabled: true,
disableSignUp: false,
requireEmailVerification: false,
minPasswordLength: 8,
advanced: {
generateId: false,
},
maxPasswordLength: 128,
autoSignIn: true,
sendResetPassword: async ({ user, url, token }) => {
console.log('Reset password URL:', url) // TODO
},
resetPasswordTokenExpiresIn: 3600, // 1 hour
password: {
hash: async (password) => {
const hashedPassword = await hash(password, 10)
console.log(password)
return hashedPassword
},
verify: async ({ hash, password }) => {
const isValid = await compare(password, hash)
return isValid
},
},
},
})
WHEN I TRY: auth.api.createUser({... I get UNAUTHORIZED
ATTEMPTED WORKAROUND
I created my user directly with prisma Note: I also added an account record that is linked to the user. But when i try to sign in the user from
my frontend I get this :
{
"code": "INVALID_EMAIL_OR_PASSWORD",
"message": "Invalid email or password"
}
My questions :
Is there a diffrent way to login admins ?? I have checked the docs but not seen any
Is it possible to seed user into the database using better-auth ?? Or do I have to do it manually and if so , how do i do it correctly
3 Replies
Hey I do have a seeding library which may be able to help you out:
https://www.better-auth-kit.com/docs/cli/seed
If you want more finegrain control over the random values in the seeding process, you can do something like this:
It's not documented yet.
Just checked it out its a great library. Though for my use-case I just need to seed a single user (Admin user). Let me try looking around if I get a solution I will post it here. If not I might have to roll my own auth