From 357fafe6b84e32f542d7d8f7d7591e6b4cb9687d Mon Sep 17 00:00:00 2001 From: Mathias Hurler Date: Sun, 30 Aug 2026 11:36:44 +0200 Subject: [PATCH] =?UTF-8?q?Start-Command=20f=C3=BCr=20Production=20hinzuf?= =?UTF-8?q?=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - scripts/seed-admin.ts | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 scripts/seed-admin.ts diff --git a/.gitignore b/.gitignore index 448bd29..8b25bb5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,3 @@ dist-ssr .vinxi __unconfig* todos.json -scripts diff --git a/scripts/seed-admin.ts b/scripts/seed-admin.ts new file mode 100644 index 0000000..eeacfdf --- /dev/null +++ b/scripts/seed-admin.ts @@ -0,0 +1,65 @@ +import { config } from 'dotenv' + +config({ path: ['.env.local', '.env'] }) + +const { eq } = await import('drizzle-orm') +const { db } = await import('#/db/index.ts') +const { account, user } = await import('#/db/schema.ts') +const { auth } = await import('#/lib/auth.ts') + +const email = process.env.SEED_ADMIN_EMAIL +const password = process.env.SEED_ADMIN_PASSWORD +const name = process.env.SEED_ADMIN_NAME ?? 'Admin' + +if (!email || !password) { + console.error( + 'Fehlende Env-Variablen: SEED_ADMIN_EMAIL und SEED_ADMIN_PASSWORD setzen.', + ) + process.exit(1) +} + +const existing = await db.query.user.findFirst({ + where: eq(user.email, email), +}) + +if (existing) { + if (existing.role !== 'admin') { + await db + .update(user) + .set({ role: 'admin', updatedAt: new Date() }) + .where(eq(user.id, existing.id)) + console.log(`Rolle fuer ${email} auf admin gesetzt.`) + } else { + console.log(`Admin ${email} existiert bereits.`) + } + process.exit(0) +} + +const ctx = await auth.$context +const hashedPassword = await ctx.password.hash(password) +const id = crypto.randomUUID() +const now = new Date() + +await db.insert(user).values({ + id, + email, + name, + emailVerified: true, + role: 'admin', + createdAt: now, + updatedAt: now, +}) + +await db.insert(account).values({ + id: crypto.randomUUID(), + accountId: id, + providerId: 'credential', + issuer: 'local:credential', + userId: id, + password: hashedPassword, + createdAt: now, + updatedAt: now, +}) + +console.log(`Admin ${email} angelegt.`) +process.exit(0)