codigo0/backend/scripts/seed-admin.js
planetazuzu 0201f16cf4
Some checks are pending
Auto Deploy to Server / deploy (push) Waiting to run
Update lab configuration 2026-03-22
2026-03-22 22:50:29 +01:00

63 lines
1.8 KiB
JavaScript
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Script para crear usuario administrador inicial
*/
import bcrypt from 'bcrypt';
import { query } from '../config/database.js';
import 'dotenv/config';
const DEFAULT_ADMIN = {
email: 'admin@emerges-tes.local',
username: 'admin',
password: 'Admin123!',
role: 'super_admin',
};
async function seedAdmin() {
try {
console.log('🌱 Creando usuario administrador...');
// Verificar si ya existe
const existing = await query(
`SELECT id FROM tes_content.users WHERE email = $1`,
[DEFAULT_ADMIN.email]
);
if (existing.rows.length > 0) {
console.log('⚠️ Usuario administrador ya existe, actualizando contraseña...');
const passwordHash = await bcrypt.hash(DEFAULT_ADMIN.password, 10);
await query(
`UPDATE tes_content.users
SET password_hash = $1, is_active = true
WHERE email = $2`,
[passwordHash, DEFAULT_ADMIN.email]
);
console.log('✅ Contraseña actualizada');
return;
}
// Hash de contraseña
const passwordHash = await bcrypt.hash(DEFAULT_ADMIN.password, 10);
// Crear usuario
await query(
`INSERT INTO tes_content.users
(id, email, username, password_hash, role, is_active)
VALUES (gen_random_uuid(), $1, $2, $3, $4, true)`,
[DEFAULT_ADMIN.email, DEFAULT_ADMIN.username, passwordHash, DEFAULT_ADMIN.role]
);
console.log('✅ Usuario administrador creado:');
console.log(` Email: ${DEFAULT_ADMIN.email}`);
console.log(` Password: ${DEFAULT_ADMIN.password}`);
console.log(` Role: ${DEFAULT_ADMIN.role}`);
console.log('\n⚠ IMPORTANTE: Cambiar la contraseña después del primer login');
} catch (error) {
console.error('❌ Error creando usuario administrador:', error);
process.exit(1);
}
}
seedAdmin();