mirror of
https://github.com/mosh-hamedani/helpdesk.git
synced 2026-05-21 11:58:19 +02:00
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
|
|
import "dotenv/config";
|
||
|
|
import { PrismaPg } from "@prisma/adapter-pg";
|
||
|
|
import { PrismaClient } from "../src/generated/prisma/client";
|
||
|
|
import { Role } from "../src/generated/prisma/client";
|
||
|
|
import { hashPassword } from "better-auth/crypto";
|
||
|
|
|
||
|
|
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
|
||
|
|
const prisma = new PrismaClient({ adapter });
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
const email = process.env.SEED_ADMIN_EMAIL;
|
||
|
|
const password = process.env.SEED_ADMIN_PASSWORD;
|
||
|
|
|
||
|
|
if (!email || !password) {
|
||
|
|
throw new Error(
|
||
|
|
"SEED_ADMIN_EMAIL and SEED_ADMIN_PASSWORD must be set in .env"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
const existing = await prisma.user.findUnique({ where: { email } });
|
||
|
|
if (existing) {
|
||
|
|
console.log(`Admin user ${email} already exists — skipping.`);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const hashedPassword = await hashPassword(password);
|
||
|
|
const userId = crypto.randomUUID();
|
||
|
|
const now = new Date();
|
||
|
|
|
||
|
|
await prisma.$transaction([
|
||
|
|
prisma.user.create({
|
||
|
|
data: {
|
||
|
|
id: userId,
|
||
|
|
name: "Admin",
|
||
|
|
email,
|
||
|
|
emailVerified: false,
|
||
|
|
role: Role.admin,
|
||
|
|
createdAt: now,
|
||
|
|
updatedAt: now,
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
prisma.account.create({
|
||
|
|
data: {
|
||
|
|
id: crypto.randomUUID(),
|
||
|
|
accountId: userId,
|
||
|
|
providerId: "credential",
|
||
|
|
userId,
|
||
|
|
password: hashedPassword,
|
||
|
|
createdAt: now,
|
||
|
|
updatedAt: now,
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
]);
|
||
|
|
|
||
|
|
console.log(`Admin user ${email} created successfully.`);
|
||
|
|
}
|
||
|
|
|
||
|
|
main()
|
||
|
|
.catch((e) => {
|
||
|
|
console.error(e);
|
||
|
|
process.exit(1);
|
||
|
|
})
|
||
|
|
.finally(() => prisma.$disconnect());
|