Add logoutDevice functionality to manage device-specific logouts
Despliegue Automático / desplegar (push) Successful in 1m12s

This commit is contained in:
2026-09-07 17:13:03 +02:00
parent be964b7501
commit 776ed86742
4 changed files with 174 additions and 284 deletions
+24 -1
View File
@@ -132,5 +132,28 @@ const getSessionsInfo = async (req, res) => {
}
}
const logoutDevice = async (req, res) => {
try {
const authorizationHeader = req.headers.authorization || '';
if (!authorizationHeader.startsWith('Bearer ')) {
return res.status(401).json({ error: 'Authorization header missing or invalid' });
}
const token = authorizationHeader.slice(7).trim();
const payload = jwt.verify(token, JWT_SECRET);
const userId = payload && payload.id;
if (!userId) return res.status(401).json({ error: 'Usuario inválido en token' });
const { id } = req.body;
if (!id) return res.status(400).json({ error: 'ID del dispositivo es obligatorio' });
const result = await authService.logoutDevice_id(userId, id);
res.json(result);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
// Exportamos los handlers de auth.
module.exports = { register, login, refresh, logout, deleteAllData, getSessionsInfo};
module.exports = { register, login, refresh, logout, deleteAllData, getSessionsInfo, logoutDevice };
+2
View File
@@ -19,4 +19,6 @@ router.delete('/delete-all-data', authController.deleteAllData);
router.get('/getSessionsInfo', authController.getSessionsInfo);
router.post('/logout-device', authController.logoutDevice); // Nueva ruta para cerrar sesión en un dispositivo específico
module.exports = router;
+9
View File
@@ -117,6 +117,15 @@ class AuthService {
return sessions;
}
async logoutDevice_id(userId, id) {
const deletedCount = await RefreshToken.destroy({ where: { id, userId } });
if (deletedCount === 0) {
throw new Error('No se encontró la sesión para cerrar');
}
return { message: 'Sesión cerrada en el dispositivo especificado correctamente.' };
}
}
module.exports = new AuthService();