feat: add admin application setup with authentication and deployment configurations
This commit is contained in:
73
scripts/ensure-admin.cjs
Normal file
73
scripts/ensure-admin.cjs
Normal file
@@ -0,0 +1,73 @@
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
const email = 'admin@afrohub.com';
|
||||
const initialPassword = process.env.ADMIN_INITIAL_PASSWORD || 'afrohub2025';
|
||||
|
||||
console.log(`🔍 Vérification du compte Super Admin (${email})...`);
|
||||
|
||||
// Auto-migration: Ensure columns adminRole and adminPermissions exist
|
||||
try {
|
||||
await prisma.$executeRawUnsafe(`
|
||||
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "adminRole" TEXT DEFAULT 'ADMIN';
|
||||
`);
|
||||
await prisma.$executeRawUnsafe(`
|
||||
ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "adminPermissions" TEXT[] DEFAULT ARRAY[]::TEXT[];
|
||||
`);
|
||||
console.log(`✅ Structure des colonnes vérifiée / mise à jour.`);
|
||||
} catch (err) {
|
||||
console.warn(`⚠️ Note sur la mise à jour des colonnes:`, err.message);
|
||||
}
|
||||
|
||||
const existing = await prisma.user.findFirst({
|
||||
where: { email: { equals: email, mode: 'insensitive' } }
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
if (existing.adminRole !== 'SUPER_ADMIN' || existing.role !== 'ADMIN' || existing.isSuspended) {
|
||||
await prisma.user.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
role: 'ADMIN',
|
||||
adminRole: 'SUPER_ADMIN',
|
||||
isSuspended: false
|
||||
}
|
||||
});
|
||||
console.log(`✅ Compte existant mis à jour en SUPER_ADMIN (${email}).`);
|
||||
} else {
|
||||
console.log(`✅ Le compte Super Admin (${email}) est déjà présent et actif.`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(initialPassword, 10);
|
||||
|
||||
const created = await prisma.user.create({
|
||||
data: {
|
||||
name: 'Admin Afrohub',
|
||||
email: email,
|
||||
password: hashedPassword,
|
||||
role: 'ADMIN',
|
||||
adminRole: 'SUPER_ADMIN',
|
||||
adminPermissions: [],
|
||||
emailVerified: true
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`🎉 Compte Super Admin créé avec succès !`);
|
||||
console.log(` Email: ${created.email}`);
|
||||
console.log(` Rôle: ${created.adminRole}`);
|
||||
console.log(` Mot de passe initial: ${process.env.ADMIN_INITIAL_PASSWORD ? '(défini via ADMIN_INITIAL_PASSWORD)' : 'afrohub2025'}`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch(err => {
|
||||
console.error("❌ Erreur lors de l'initialisation du Super Admin:", err);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user