66 lines
1.5 KiB
TypeScript
66 lines
1.5 KiB
TypeScript
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)
|