Files
2025-12-27 21:03:20 +01:00

78 lines
2.0 KiB
TypeScript

import { PrismaClient } from "@/generated/prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import { hashPassword } from "better-auth/crypto";
const connectionString = `${process.env.DATABASE_URL}`
const adapter = new PrismaPg({ connectionString })
const prisma = new PrismaClient({ adapter })
function uid() {
return crypto.randomUUID();
}
async function main() {
const email = "admin@gaertan.art";
const name = "Admin";
const password = "037Ikk7qmCamW5iYBimcwiPXNELzktIRG9ndiIkA3u";
const passwordHash = await hashPassword(password);
// 1) Ensure user exists (your User.id has no default, so we must set it)
const user = await prisma.user.upsert({
where: { email },
update: {
name,
// optional: mark verified for initial admin
emailVerified: true,
},
create: {
id: uid(),
email,
name,
emailVerified: true,
},
});
// 2) Ensure credential account exists for this user
// Better Auth expects providerId="credential" and accountId=userId for credential accounts. :contentReference[oaicite:2]{index=2}
const existingCredential = await prisma.account.findFirst({
where: {
userId: user.id,
providerId: "credential",
accountId: user.id,
},
});
if (!existingCredential) {
await prisma.account.create({
data: {
id: uid(),
userId: user.id,
providerId: "credential",
accountId: user.id,
password: passwordHash,
},
});
} else {
// If it exists, keep it consistent (optionally reset password on seed)
await prisma.account.update({
where: { id: existingCredential.id },
data: { password: passwordHash },
});
}
console.log("Seeded/updated admin user:");
console.log(" email:", email);
console.log(" password:", password);
}
main()
.catch((err) => {
console.error("Seed failed:", err);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});