52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { PrismaClient, Role } from "@prisma/client";
|
|
import { PrismaPg } from "@prisma/adapter-pg";
|
|
import * as argon2 from "argon2";
|
|
import { randomBytes } from "node:crypto";
|
|
|
|
async function main(): Promise<void> {
|
|
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
const adminEmail = "admin@sso.local";
|
|
const adminPassword = "admin123!";
|
|
|
|
const existingAdmin = await prisma.user.findUnique({ where: { email: adminEmail } });
|
|
if (!existingAdmin) {
|
|
const passwordHash = await argon2.hash(adminPassword);
|
|
await prisma.user.create({
|
|
data: {
|
|
email: adminEmail,
|
|
passwordHash,
|
|
displayName: "SSO Admin",
|
|
role: Role.ADMIN,
|
|
},
|
|
});
|
|
console.log(`Admin user created: ${adminEmail} / ${adminPassword}`);
|
|
}
|
|
|
|
const testClientId = "test-client";
|
|
const existingClient = await prisma.client.findUnique({ where: { clientId: testClientId } });
|
|
if (!existingClient) {
|
|
const clientSecret = randomBytes(32).toString("hex");
|
|
await prisma.client.create({
|
|
data: {
|
|
name: "Test OAuth Client",
|
|
clientId: testClientId,
|
|
clientSecret,
|
|
redirectUris: ["http://localhost:5173/callback", "http://localhost:3010/callback"],
|
|
grants: ["authorization_code", "client_credentials", "refresh_token"],
|
|
isConfidential: true,
|
|
},
|
|
});
|
|
console.log(`OAuth client created: ${testClientId} / ${clientSecret}`);
|
|
}
|
|
|
|
console.log("Seed complete.");
|
|
await prisma.$disconnect();
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error("Seed failed:", e);
|
|
process.exit(1);
|
|
});
|