From ea666625709c2b1a6115b0166b5351ee1851d1f0 Mon Sep 17 00:00:00 2001 From: pablinux Date: Sun, 1 Nov 2026 10:02:51 -0500 Subject: [PATCH] feat(auth): promover admin@telcotronics.com a superadmin, depurar usuarios dummy y unificar login WebControl/usuario --- config/setup.js | 7 ++---- routes/auth.js | 59 +++++++++++++++++++++++++++++++++++++++++--- views/auth/login.ejs | 8 +++--- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/config/setup.js b/config/setup.js index 1318510..8cd13ec 100644 --- a/config/setup.js +++ b/config/setup.js @@ -369,12 +369,9 @@ async function setup() { const bcrypt = require('bcryptjs'); const hash = await bcrypt.hash('123456', 10); const users = [ - [1, 'super@helpdesk.com', 'Super Admin', 'superadmin'], - [1, 'admin@telcotronics.local', 'Admin Telcotronics', 'admin'], + [1, 'admin@telcotronics.com', 'PABLO FARIAS', 'superadmin'], [2, 'admin@xsystem.com', 'Admin XSYSTEM', 'admin'], - [3, 'admin@domus.com', 'Admin DOMUS', 'admin'], - [1, 'agent@helpdesk.com', 'Agente', 'agent'], - [1, 'client@helpdesk.com', 'Cliente', 'client'] + [3, 'admin@domus.com', 'Admin DOMUS', 'admin'] ]; for (const [cId, email, nombre, rol] of users) { const [u] = await conn.query('SELECT id, rol FROM users WHERE email = ?', [email]); diff --git a/routes/auth.js b/routes/auth.js index 5293b46..82859de 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -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') { diff --git a/views/auth/login.ejs b/views/auth/login.ejs index 968704d..37d015e 100644 --- a/views/auth/login.ejs +++ b/views/auth/login.ejs @@ -128,10 +128,10 @@
- +
@@ -220,10 +220,10 @@
- +