63 lines
1.8 KiB
JavaScript
Executable file
63 lines
1.8 KiB
JavaScript
Executable file
/**
|
||
* 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();
|
||
|