feat(auth): enrutar clientes webcontrol a XSYSTEM y google a Telcotronics; registrar admins por empresa
This commit is contained in:
+14
-11
@@ -162,26 +162,29 @@ async function setup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seed users
|
// Seed users and company admins
|
||||||
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 = [
|
||||||
['super@helpdesk.com', 'Super Admin', 'superadmin'],
|
[1, 'super@helpdesk.com', 'Super Admin', 'superadmin'],
|
||||||
['admin@helpdesk.com', 'Admin', 'admin'],
|
[1, 'admin@telcotronics.local', 'Admin Telcotronics', 'admin'],
|
||||||
['agent@helpdesk.com', 'Agente', 'agent'],
|
[2, 'admin@xsystem.com', 'Admin XSYSTEM', 'admin'],
|
||||||
['client@helpdesk.com', 'Cliente', 'client']
|
[3, 'admin@domus.com', 'Admin DOMUS', 'admin'],
|
||||||
|
[1, 'agent@helpdesk.com', 'Agente', 'agent'],
|
||||||
|
[1, 'client@helpdesk.com', 'Cliente', 'client']
|
||||||
];
|
];
|
||||||
for (const [email, nombre, rol] of users) {
|
for (const [cId, email, nombre, rol] of users) {
|
||||||
const [u] = await conn.query('SELECT id FROM users WHERE email = ?', [email]);
|
const [u] = await conn.query('SELECT id, rol FROM users WHERE email = ?', [email]);
|
||||||
if (u.length === 0) {
|
if (u.length === 0) {
|
||||||
await conn.query(
|
await conn.query(
|
||||||
'INSERT INTO users (company_id, nombre, email, password, rol) VALUES (?, ?, ?, ?, ?)',
|
'INSERT INTO users (company_id, nombre, email, password, rol) VALUES (?, ?, ?, ?, ?)',
|
||||||
[companyId, nombre, email, hash, rol]
|
[cId, nombre, email, hash, rol]
|
||||||
);
|
);
|
||||||
console.log(`+ Usuario ${email} creado`);
|
console.log(`+ Usuario ${email} (${rol}) creado en empresa ${cId}`);
|
||||||
} else {
|
} else {
|
||||||
await conn.query('UPDATE users SET company_id = ?, rol = ? WHERE email = ? AND rol != ?', [companyId, rol, email, 'superadmin']);
|
const finalRole = u[0].rol === 'superadmin' ? 'superadmin' : rol;
|
||||||
console.log(`~ Usuario ${email} actualizado`);
|
await conn.query('UPDATE users SET company_id = ?, nombre = ?, rol = ? WHERE email = ?', [cId, nombre, finalRole, email]);
|
||||||
|
console.log(`~ Usuario ${email} actualizado en empresa ${cId}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+26
-6
@@ -94,8 +94,8 @@ router.post('/login', async (req, res) => {
|
|||||||
const userName = wcUser.nombre || wcUser.usuario || cleanEmail.split('@')[0];
|
const userName = wcUser.nombre || wcUser.usuario || cleanEmail.split('@')[0];
|
||||||
const providerId = String(wcUser.id || cleanEmail);
|
const providerId = String(wcUser.id || cleanEmail);
|
||||||
|
|
||||||
// Sincronizar o aprovisionar usuario en help_desk.users con rol 'client'
|
// Sincronizar o aprovisionar usuario en help_desk.users con rol 'client' en XSYSTEM (id: 2)
|
||||||
const user = await User.upsertExternal(userName, cleanEmail, 'webcontrol', providerId, null, 1);
|
const user = await User.upsertExternal(userName, cleanEmail, 'webcontrol', providerId, null, 2);
|
||||||
|
|
||||||
req.session.user = {
|
req.session.user = {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
@@ -107,6 +107,12 @@ 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') {
|
||||||
|
return res.redirect('/admin/dashboard');
|
||||||
|
}
|
||||||
|
if (user.rol === 'agent') {
|
||||||
|
return res.redirect('/agent/dashboard');
|
||||||
|
}
|
||||||
return res.redirect('/client/dashboard');
|
return res.redirect('/client/dashboard');
|
||||||
} else {
|
} else {
|
||||||
// Verificación de cortesía: si un técnico intentó ingresar en la pestaña de clientes
|
// Verificación de cortesía: si un técnico intentó ingresar en la pestaña de clientes
|
||||||
@@ -159,7 +165,7 @@ router.post('/auth/google', async (req, res) => {
|
|||||||
const avatarUrl = payload.picture || null;
|
const avatarUrl = payload.picture || null;
|
||||||
const providerId = payload.sub;
|
const providerId = payload.sub;
|
||||||
|
|
||||||
// Auto-provisioning automático como cliente
|
// Auto-provisioning automático como cliente en Telcotronics (id: 1)
|
||||||
const user = await User.upsertExternal(userName, cleanEmail, 'google', providerId, avatarUrl, 1);
|
const user = await User.upsertExternal(userName, cleanEmail, 'google', providerId, avatarUrl, 1);
|
||||||
|
|
||||||
req.session.user = {
|
req.session.user = {
|
||||||
@@ -172,7 +178,14 @@ router.post('/auth/google', async (req, res) => {
|
|||||||
avatar_url: avatarUrl
|
avatar_url: avatarUrl
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.json({ success: true, redirect: '/client/dashboard' });
|
let redirectUrl = '/client/dashboard';
|
||||||
|
if (user.rol === 'superadmin' || user.rol === 'admin') {
|
||||||
|
redirectUrl = '/admin/dashboard';
|
||||||
|
} else if (user.rol === 'agent') {
|
||||||
|
redirectUrl = '/agent/dashboard';
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json({ success: true, redirect: redirectUrl });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error Google OAuth:', err.message);
|
console.error('Error Google OAuth:', err.message);
|
||||||
return res.status(500).json({ success: false, message: 'Error al verificar autenticación con Google' });
|
return res.status(500).json({ success: false, message: 'Error al verificar autenticación con Google' });
|
||||||
@@ -203,7 +216,7 @@ router.post('/auth/facebook', async (req, res) => {
|
|||||||
const avatarUrl = fbData.picture?.data?.url || null;
|
const avatarUrl = fbData.picture?.data?.url || null;
|
||||||
const providerId = fbData.id;
|
const providerId = fbData.id;
|
||||||
|
|
||||||
// Auto-provisioning automático como cliente
|
// Auto-provisioning automático como cliente en Telcotronics (id: 1)
|
||||||
const user = await User.upsertExternal(userName, cleanEmail, 'facebook', providerId, avatarUrl, 1);
|
const user = await User.upsertExternal(userName, cleanEmail, 'facebook', providerId, avatarUrl, 1);
|
||||||
|
|
||||||
req.session.user = {
|
req.session.user = {
|
||||||
@@ -216,7 +229,14 @@ router.post('/auth/facebook', async (req, res) => {
|
|||||||
avatar_url: avatarUrl
|
avatar_url: avatarUrl
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.json({ success: true, redirect: '/client/dashboard' });
|
let redirectUrl = '/client/dashboard';
|
||||||
|
if (user.rol === 'superadmin' || user.rol === 'admin') {
|
||||||
|
redirectUrl = '/admin/dashboard';
|
||||||
|
} else if (user.rol === 'agent') {
|
||||||
|
redirectUrl = '/agent/dashboard';
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json({ success: true, redirect: redirectUrl });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error Facebook OAuth:', err.message);
|
console.error('Error Facebook OAuth:', err.message);
|
||||||
return res.status(500).json({ success: false, message: 'Error al verificar autenticación con Facebook' });
|
return res.status(500).json({ success: false, message: 'Error al verificar autenticación con Facebook' });
|
||||||
|
|||||||
Reference in New Issue
Block a user