chore: add scripts/seed-admin.ts
This commit is contained in:
parent
f520592d1f
commit
893ad6558d
1 changed files with 33 additions and 0 deletions
33
scripts/seed-admin.ts
Normal file
33
scripts/seed-admin.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
/**
|
||||||
|
* Seed script — creates the first admin user.
|
||||||
|
* Run: npm run db:seed
|
||||||
|
*
|
||||||
|
* It reads ADMIN_EMAIL and ADMIN_PASSWORD from environment variables,
|
||||||
|
* or falls back to admin@example.com / changeme.
|
||||||
|
*
|
||||||
|
* Usage in Replit Secrets:
|
||||||
|
* ADMIN_EMAIL=you@yourdomain.com
|
||||||
|
* ADMIN_PASSWORD=your-secure-password
|
||||||
|
*/
|
||||||
|
import "dotenv/config";
|
||||||
|
import bcrypt from "bcrypt";
|
||||||
|
import { db } from "../server/db";
|
||||||
|
import { adminUsers } from "../shared/schema";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
|
const email = process.env.ADMIN_EMAIL || "admin@example.com";
|
||||||
|
const password = process.env.ADMIN_PASSWORD || "changeme";
|
||||||
|
|
||||||
|
async function seed() {
|
||||||
|
const existing = await db.select().from(adminUsers).where(eq(adminUsers.email, email));
|
||||||
|
if (existing.length) {
|
||||||
|
console.log(`Admin ${email} already exists.`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
const passwordHash = await bcrypt.hash(password, 12);
|
||||||
|
await db.insert(adminUsers).values({ email, passwordHash, role: "superadmin" });
|
||||||
|
console.log(`✓ Admin created: ${email}`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
seed().catch((err) => { console.error(err); process.exit(1); });
|
||||||
Loading…
Add table
Reference in a new issue