46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
require('dotenv').config()
|
|
|
|
const bcrypt = require('bcrypt')
|
|
const prisma = require('../config/prisma')
|
|
|
|
async function createAdmin() {
|
|
const email = process.env.ADMIN_EMAIL
|
|
const password = process.env.ADMIN_PASSWORD
|
|
|
|
if (!email || !password) {
|
|
throw new Error('ADMIN_EMAIL et ADMIN_PASSWORD doivent être définis')
|
|
}
|
|
|
|
const existing = await prisma.user.findUnique({ where: { email } })
|
|
|
|
if (existing) {
|
|
await prisma.user.update({
|
|
where: { email },
|
|
data: { isEmailVerified: true, role: 'ADMIN' },
|
|
})
|
|
console.log('Admin mis à jour :', email)
|
|
return
|
|
}
|
|
|
|
const hashed = await bcrypt.hash(password, 10)
|
|
|
|
await prisma.user.create({
|
|
data: {
|
|
firstname: 'Admin',
|
|
lastname: 'TipTop',
|
|
email,
|
|
password: hashed,
|
|
role: 'ADMIN',
|
|
isEmailVerified: true,
|
|
},
|
|
})
|
|
|
|
console.log('Admin créé avec succès')
|
|
console.log(' Email :', email)
|
|
console.log(' Mot de passe:', password)
|
|
}
|
|
|
|
createAdmin()
|
|
.catch((e) => { console.error('Erreur :', e.message); process.exit(1) })
|
|
.finally(() => prisma.$disconnect())
|