feat(auth): enrutar clientes webcontrol a XSYSTEM y google a Telcotronics; registrar admins por empresa
This commit is contained in:
+26
-6
@@ -94,8 +94,8 @@ router.post('/login', async (req, res) => {
|
||||
const userName = wcUser.nombre || wcUser.usuario || cleanEmail.split('@')[0];
|
||||
const providerId = String(wcUser.id || cleanEmail);
|
||||
|
||||
// Sincronizar o aprovisionar usuario en help_desk.users con rol 'client'
|
||||
const user = await User.upsertExternal(userName, cleanEmail, 'webcontrol', providerId, null, 1);
|
||||
// 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, 2);
|
||||
|
||||
req.session.user = {
|
||||
id: user.id,
|
||||
@@ -107,6 +107,12 @@ 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') {
|
||||
return res.redirect('/admin/dashboard');
|
||||
}
|
||||
if (user.rol === 'agent') {
|
||||
return res.redirect('/agent/dashboard');
|
||||
}
|
||||
return res.redirect('/client/dashboard');
|
||||
} else {
|
||||
// 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 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);
|
||||
|
||||
req.session.user = {
|
||||
@@ -172,7 +178,14 @@ router.post('/auth/google', async (req, res) => {
|
||||
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) {
|
||||
console.error('Error Google OAuth:', err.message);
|
||||
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 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);
|
||||
|
||||
req.session.user = {
|
||||
@@ -216,7 +229,14 @@ router.post('/auth/facebook', async (req, res) => {
|
||||
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) {
|
||||
console.error('Error Facebook OAuth:', err.message);
|
||||
return res.status(500).json({ success: false, message: 'Error al verificar autenticación con Facebook' });
|
||||
|
||||
Reference in New Issue
Block a user