From 8f438901611b7fe304bef5eba9d79fb5bdf386f3 Mon Sep 17 00:00:00 2001 From: Marcos Date: Thu, 20 Aug 2026 15:45:07 +0200 Subject: [PATCH] Add getSessionsInfo method to retrieve user session details --- src/controllers/authController.js | 24 ++++++++++++++++++++++++ src/services/authService.js | 13 +++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/controllers/authController.js b/src/controllers/authController.js index 3c5e12c..c143362 100644 --- a/src/controllers/authController.js +++ b/src/controllers/authController.js @@ -108,5 +108,29 @@ const deleteAllData = async (req, res) => { } }; +const getSessionsInfo = 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 result = await authService.getSessionsInfo(userId); + + res.json(result); + } catch (error) { + if (error.name === 'JsonWebTokenError' || error.name === 'TokenExpiredError') { + return res.status(401).json({ error: 'Token inválido o expirado' }); + } + res.status(500).json({ error: error.message }); + } +} + // Exportamos los handlers de auth. module.exports = { register, login, refresh, logout, deleteAllData }; \ No newline at end of file diff --git a/src/services/authService.js b/src/services/authService.js index 0325a28..34ec17e 100644 --- a/src/services/authService.js +++ b/src/services/authService.js @@ -104,6 +104,19 @@ class AuthService { }; }); } + + async getSessionsInfo(userId) { + if (!userId) { + throw new Error('userId es obligatorio'); + } + + const sessions = await RefreshToken.findAll({ + where: { userId }, + attributes: ['id', 'deviceName', 'createdAt', 'updatedAt'] + }); + + return sessions; + } } module.exports = new AuthService(); \ No newline at end of file