47 lines
1012 B
JavaScript
47 lines
1012 B
JavaScript
const { PrismaClient } = require('@prisma/client')
|
|
const bcrypt = require('bcryptjs')
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
async function main() {
|
|
const email = process.env.INITIAL_ADMIN_EMAIL
|
|
const password = process.env.INITIAL_ADMIN_PASSWORD
|
|
|
|
if (!email || !password) {
|
|
console.log('INITIAL_ADMIN_EMAIL or INITIAL_ADMIN_PASSWORD not set, skipping bootstrap')
|
|
return
|
|
}
|
|
|
|
const ownerCount = await prisma.user.count({
|
|
where: { role: 'OWNER' },
|
|
})
|
|
|
|
if (ownerCount > 0) {
|
|
console.log('Owner already exists, skipping bootstrap')
|
|
return
|
|
}
|
|
|
|
const passwordHash = await bcrypt.hash(password, 12)
|
|
|
|
const admin = await prisma.user.create({
|
|
data: {
|
|
email,
|
|
passwordHash,
|
|
role: 'OWNER',
|
|
name: 'Admin',
|
|
mustChangePassword: true,
|
|
},
|
|
})
|
|
|
|
console.log(`Created owner account: ${admin.email}`)
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('Bootstrap failed:', e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|