24 lines
921 B
TypeScript
24 lines
921 B
TypeScript
import { getDb } from "./db";
|
|
|
|
/**
|
|
* Legt beim allerersten Start das Admin-Konto aus dem bisherigen
|
|
* .env-Login an (APP_USER/APP_PASSWORD_HASH, optionale ADMIN_EMAIL).
|
|
* Läuft nur, wenn noch kein Nutzer existiert.
|
|
*/
|
|
export async function ensureFirstAdmin(): Promise<void> {
|
|
try {
|
|
const db = getDb();
|
|
const count = await db.user.count();
|
|
if (count > 0) return;
|
|
const name = (process.env.APP_USER ?? "").trim();
|
|
const hash = process.env.APP_PASSWORD_HASH ?? "";
|
|
if (!name || !hash) return;
|
|
const rawMail = (process.env.ADMIN_EMAIL ?? "").trim().toLowerCase();
|
|
const email = /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(rawMail) ? rawMail : null;
|
|
await db.user.create({ data: { name, email, passwordHash: hash, role: "admin" } });
|
|
console.log(`Erster Admin angelegt (${name}).`);
|
|
} catch (err) {
|
|
console.error("ensureFirstAdmin fehlgeschlagen (Start läuft weiter):", err);
|
|
}
|
|
}
|