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
+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');
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)
// =========================================================================
if (login_type === 'staff') {
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) {
req.flash('error_msg', 'Credenciales de personal técnico incorrectas');
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');
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) {
req.flash('error_msg', 'Credenciales de personal técnico incorrectas');
return res.redirect('/login?tab=staff');
@@ -58,6 +103,9 @@ router.post('/login', async (req, res) => {
provider: user.provider || 'local'
};
req.flash('success_msg', `Bienvenido al panel operativo, ${user.nombre}`);
if (user.rol === 'superadmin') {
return res.redirect('/saas/companies');
}
return res.redirect('/');
} catch (error) {
console.error('Error login staff:', error);
@@ -107,7 +155,10 @@ router.post('/login', async (req, res) => {
webcontrol_token: wcData.token || null
};
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');
}
if (user.rol === 'agent') {