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
5 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
@ab hi, did you find a solution to this?
@ab @Jim-Y
Hi I just had this issue and came across this thread so thought I'd share what I did to be able to seed an admin user and have it work when logging in from frontend without getting the invalid email or password error. I'm using prisma and Next js. Setting the password I had followed the documentation at https://www.better-auth.com/docs/authentication/email-password.