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
Ping
Ping4w ago
Hey I do have a seeding library which may be able to help you out: https://www.better-auth-kit.com/docs/cli/seed
Better Auth Kit
Better Auth Kit
A handy collection of plugins, adapters, libraries and more.
Ping
Ping4w ago
If you want more finegrain control over the random values in the seeding process, you can do something like this:
export const seed = Seed(async ({ get }) => {
const first_name = await get($.firstname());
const last_name = await get($.lastname());

const isBanned = await get($.randomBoolean({ probability: 0.1 }));


return {
test: table({
name: $.custom(() => `${first_name} ${last_name}`),
first_name: $.custom(() => first_name),
last_name: $.custom(() => last_name),

banned: $.custom(() => isBanned),
banReason: $.custom(() => isBanned ? "test" : null),
banExpires: $.custom(() => isBanned ? get($.randomDate("future")) : null),
}),
};
});
export const seed = Seed(async ({ get }) => {
const first_name = await get($.firstname());
const last_name = await get($.lastname());

const isBanned = await get($.randomBoolean({ probability: 0.1 }));


return {
test: table({
name: $.custom(() => `${first_name} ${last_name}`),
first_name: $.custom(() => first_name),
last_name: $.custom(() => last_name),

banned: $.custom(() => isBanned),
banReason: $.custom(() => isBanned ? "test" : null),
banExpires: $.custom(() => isBanned ? get($.randomDate("future")) : null),
}),
};
});
It's not documented yet.
ab
abOP4w ago
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

Did you find this page helpful?