creating the first user, where disableSignUp is true

Hi, are there any suggestions on how to do this? I've created the DB schema fine but with this off and no API in place yet I was hoping there'd be a CLI command
2 Replies
Stormlight
Stormlight2mo ago
You can use the db adapter to seed the database with the admin user. I'm using drizzle-orm and I've created a seed.ts file, which I then ran using "bun run server/src/db/seed.ts". If you use a different adapter then you'll need to check their docs on seeding the database. Here is the script:
import { db } from "./index";
import { user, account } from "./auth-schema";
import { eq } from "drizzle-orm";
import { hashPassword } from "better-auth/crypto";

// Seed the database with admin user
async function main() {
const adminEmail = process.env.ADMIN_EMAIL || "[email protected]";
const adminUser = await db.select().from(user).where(eq(user.email, adminEmail));
if (adminUser.length > 0) {
console.log("Admin user already exists");
return;
}
console.log("Seeding the database with the initial data");
try {
const adminUser = {
"id": "app-admin",
"name": "Admin",
"email": adminEmail,
"emailVerified": true,
"role": "admin",
"createdAt": new Date(),
"updatedAt": new Date(),
}

const adminAccount = {
"id": "app-admin-main-account",
"accountId": "app-admin",
"providerId": "credential",
"userId": "app-admin",
"accessToken": null,
"refreshToken": null,
"idToken": null,
"accessTokenExpiresAt": null,
"refreshTokenExpiresAt": null,
"scope": null,
"password": await hashPassword(process.env.ADMIN_PASSWORD || "password123"),
"createdAt": new Date(),
"updatedAt": new Date()
}

await db.insert(user).values(adminUser);
await db.insert(account).values(adminAccount);
console.log("Admin user created successfully");
} catch (error) {
console.error("Error creating admin user:", error);
}
}

main();
import { db } from "./index";
import { user, account } from "./auth-schema";
import { eq } from "drizzle-orm";
import { hashPassword } from "better-auth/crypto";

// Seed the database with admin user
async function main() {
const adminEmail = process.env.ADMIN_EMAIL || "[email protected]";
const adminUser = await db.select().from(user).where(eq(user.email, adminEmail));
if (adminUser.length > 0) {
console.log("Admin user already exists");
return;
}
console.log("Seeding the database with the initial data");
try {
const adminUser = {
"id": "app-admin",
"name": "Admin",
"email": adminEmail,
"emailVerified": true,
"role": "admin",
"createdAt": new Date(),
"updatedAt": new Date(),
}

const adminAccount = {
"id": "app-admin-main-account",
"accountId": "app-admin",
"providerId": "credential",
"userId": "app-admin",
"accessToken": null,
"refreshToken": null,
"idToken": null,
"accessTokenExpiresAt": null,
"refreshTokenExpiresAt": null,
"scope": null,
"password": await hashPassword(process.env.ADMIN_PASSWORD || "password123"),
"createdAt": new Date(),
"updatedAt": new Date()
}

await db.insert(user).values(adminUser);
await db.insert(account).values(adminAccount);
console.log("Admin user created successfully");
} catch (error) {
console.error("Error creating admin user:", error);
}
}

main();
je823
je823OP2mo ago
amazing, thank you for sharing this!

Did you find this page helpful?