Files
XBid/scripts/create-user.ts
twotalesanimation 9dcf310806
Deploy / deploy (push) Successful in 2m3s
User Access
2026-08-18 15:23:22 +02:00

51 lines
1.2 KiB
TypeScript

import { config } from "dotenv";
config();
import { PrismaClient } from "@prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import bcrypt from "bcryptjs";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! });
const prisma = new PrismaClient({ adapter } as any);
async function main() {
const [, , email, password, name] = process.argv;
if (!email || !password) {
console.error("Usage: npm run db:create-user -- <email> <password> [name]");
process.exit(1);
}
if (password.length < 8) {
console.error("Password must be at least 8 characters long.");
process.exit(1);
}
const hashedPassword = await bcrypt.hash(password, 12);
const normalizedEmail = email.toLowerCase().trim();
const user = await prisma.user.upsert({
where: { email: normalizedEmail },
create: {
email: normalizedEmail,
password: hashedPassword,
name: name ?? null,
role: "ADMIN",
},
update: {
password: hashedPassword,
...(name ? { name } : {}),
},
});
console.log(`✅ User ready: ${user.email} (role: ${user.role})`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});