feat(auth): promover admin@telcotronics.com a superadmin, depurar usuarios dummy y unificar login WebControl/usuario

This commit is contained in:
2026-11-01 10:02:51 -05:00
parent 82b1a52524
commit ea66662570
3 changed files with 61 additions and 13 deletions
+2 -5
View File
@@ -369,12 +369,9 @@ async function setup() {
const bcrypt = require('bcryptjs'); const bcrypt = require('bcryptjs');
const hash = await bcrypt.hash('123456', 10); const hash = await bcrypt.hash('123456', 10);
const users = [ const users = [
[1, 'super@helpdesk.com', 'Super Admin', 'superadmin'], [1, 'admin@telcotronics.com', 'PABLO FARIAS', 'superadmin'],
[1, 'admin@telcotronics.local', 'Admin Telcotronics', 'admin'],
[2, 'admin@xsystem.com', 'Admin XSYSTEM', 'admin'], [2, 'admin@xsystem.com', 'Admin XSYSTEM', 'admin'],
[3, 'admin@domus.com', 'Admin DOMUS', 'admin'], [3, 'admin@domus.com', 'Admin DOMUS', 'admin']
[1, 'agent@helpdesk.com', 'Agente', 'agent'],
[1, 'client@helpdesk.com', 'Cliente', 'client']
]; ];
for (const [cId, email, nombre, rol] of users) { for (const [cId, email, nombre, rol] of users) {
const [u] = await conn.query('SELECT id, rol FROM users WHERE email = ?', [email]); const [u] = await conn.query('SELECT id, rol FROM users WHERE email = ?', [email]);
+55 -4
View File
@@ -18,14 +18,27 @@ router.post('/login', async (req, res) => {
req.flash('error_msg', 'Por favor ingresa tu correo y contraseña'); req.flash('error_msg', 'Por favor ingresa tu correo y contraseña');
return res.redirect('/login'); return res.redirect('/login');
} }
const cleanEmail = email.trim().toLowerCase(); const rawInput = email.trim();
let cleanEmail = rawInput.toLowerCase();
// ========================================================================= // =========================================================================
// 1. MODO PERSONAL TÉCNICO & STAFF (Superadmin, Admins y Agentes en MariaDB) // 1. MODO PERSONAL TÉCNICO & STAFF (Superadmin, Admins y Agentes en MariaDB)
// ========================================================================= // =========================================================================
if (login_type === 'staff') { if (login_type === 'staff') {
try { try {
const user = await User.findByEmail(cleanEmail); let lookupEmail = cleanEmail;
if (!lookupEmail.includes('@')) {
const db = require('../config/db');
const [matched] = await db.query(
'SELECT email FROM users WHERE email LIKE ? OR nombre LIKE ? LIMIT 1',
[`${cleanEmail}@%`, `%${cleanEmail}%`]
);
if (matched.length > 0) {
lookupEmail = matched[0].email;
}
}
const user = await User.findByEmail(lookupEmail);
if (!user) { if (!user) {
req.flash('error_msg', 'Credenciales de personal técnico incorrectas'); req.flash('error_msg', 'Credenciales de personal técnico incorrectas');
return res.redirect('/login?tab=staff'); return res.redirect('/login?tab=staff');
@@ -44,7 +57,39 @@ router.post('/login', async (req, res) => {
req.flash('error_msg', 'Tu organización se encuentra suspendida'); req.flash('error_msg', 'Tu organización se encuentra suspendida');
return res.redirect('/login?tab=staff'); return res.redirect('/login?tab=staff');
} }
const match = await bcrypt.compare(password, user.password);
let match = false;
if (user.password) {
match = await bcrypt.compare(password, user.password);
}
// Fallback a WebControl si no coincide la clave local o es usuario WebControl
if (!match) {
try {
const webcontrolUrl = process.env.WEBCONTROL_API_URL || 'http://192.168.10.160:3002';
let wcRes;
try {
wcRes = await fetch(`${webcontrolUrl}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: lookupEmail, paswd: password })
});
} catch (fetchErr) {
wcRes = await fetch('https://api.siax-system.net/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: lookupEmail, paswd: password })
});
}
const wcData = await wcRes.json();
if (wcRes.ok && wcData.success) {
match = true;
}
} catch (wcErr) {
console.warn('[Login staff WebControl fallback]:', wcErr.message);
}
}
if (!match) { if (!match) {
req.flash('error_msg', 'Credenciales de personal técnico incorrectas'); req.flash('error_msg', 'Credenciales de personal técnico incorrectas');
return res.redirect('/login?tab=staff'); return res.redirect('/login?tab=staff');
@@ -58,6 +103,9 @@ router.post('/login', async (req, res) => {
provider: user.provider || 'local' provider: user.provider || 'local'
}; };
req.flash('success_msg', `Bienvenido al panel operativo, ${user.nombre}`); req.flash('success_msg', `Bienvenido al panel operativo, ${user.nombre}`);
if (user.rol === 'superadmin') {
return res.redirect('/saas/companies');
}
return res.redirect('/'); return res.redirect('/');
} catch (error) { } catch (error) {
console.error('Error login staff:', error); console.error('Error login staff:', error);
@@ -107,7 +155,10 @@ router.post('/login', async (req, res) => {
webcontrol_token: wcData.token || null webcontrol_token: wcData.token || null
}; };
req.flash('success_msg', `Bienvenido a Help Desk, ${user.nombre}`); req.flash('success_msg', `Bienvenido a Help Desk, ${user.nombre}`);
if (user.rol === 'superadmin' || user.rol === 'admin') { if (user.rol === 'superadmin') {
return res.redirect('/saas/companies');
}
if (user.rol === 'admin') {
return res.redirect('/admin/dashboard'); return res.redirect('/admin/dashboard');
} }
if (user.rol === 'agent') { if (user.rol === 'agent') {
+4 -4
View File
@@ -128,10 +128,10 @@
<input type="hidden" name="login_type" value="client"> <input type="hidden" name="login_type" value="client">
<div class="mb-3"> <div class="mb-3">
<label class="form-label text-secondary small fw-semibold">Correo de Cliente / Empresa</label> <label class="form-label text-secondary small fw-semibold">Correo o Usuario WebControl</label>
<div class="input-group login-input-group"> <div class="input-group login-input-group">
<span class="input-group-text"><i class="fas fa-envelope"></i></span> <span class="input-group-text"><i class="fas fa-envelope"></i></span>
<input type="email" name="email" id="clientEmail" class="form-control" placeholder="tu-correo@empresa.com" required autocomplete="email"> <input type="text" name="email" id="clientEmail" class="form-control" placeholder="admin@telcotronics.com o usuario" required autocomplete="username">
</div> </div>
</div> </div>
@@ -220,10 +220,10 @@
<input type="hidden" name="login_type" value="staff"> <input type="hidden" name="login_type" value="staff">
<div class="mb-3"> <div class="mb-3">
<label class="form-label text-secondary small fw-semibold">Correo de Operaciones</label> <label class="form-label text-secondary small fw-semibold">Correo o Usuario Técnico</label>
<div class="input-group login-input-group"> <div class="input-group login-input-group">
<span class="input-group-text"><i class="fas fa-id-badge"></i></span> <span class="input-group-text"><i class="fas fa-id-badge"></i></span>
<input type="email" name="email" id="staffEmail" class="form-control" placeholder="agente@helpdesk.com" required autocomplete="email"> <input type="text" name="email" id="staffEmail" class="form-control" placeholder="admin@telcotronics.com o usuario" required autocomplete="username">
</div> </div>
</div> </div>