46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
/**
|
|
* Seed script: creates an initial admin user.
|
|
* Usage: npm run db:seed
|
|
*
|
|
* Set ADMIN_USERNAME and ADMIN_PASSWORD env vars, or use the defaults below.
|
|
* Change the defaults before running in production.
|
|
*/
|
|
import { PrismaClient } from '@prisma/client'
|
|
import bcrypt from 'bcryptjs'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
async function main() {
|
|
const username = process.env.ADMIN_USERNAME ?? 'admin'
|
|
const password = process.env.ADMIN_PASSWORD ?? 'changeme123'
|
|
|
|
const existing = await prisma.user.findUnique({ where: { username } })
|
|
if (existing) {
|
|
console.log(`User "${username}" already exists — skipping seed.`)
|
|
return
|
|
}
|
|
|
|
const passwordHash = await bcrypt.hash(password, 12)
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
username,
|
|
passwordHash,
|
|
isAdmin: true,
|
|
balance: 10000, // admins start with extra for testing
|
|
researchPoints: 10,
|
|
},
|
|
})
|
|
|
|
console.log(`Created admin user: ${user.username} (id: ${user.id})`)
|
|
console.log('Remember to change the password after first login!')
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|