Add getSessionsInfo method to retrieve user session details
Despliegue Automático / desplegar (push) Successful in 1m24s

This commit is contained in:
2026-08-20 15:45:07 +02:00
parent 0de97d3a00
commit 8f43890161
2 changed files with 37 additions and 0 deletions
+24
View File
@@ -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. // Exportamos los handlers de auth.
module.exports = { register, login, refresh, logout, deleteAllData }; module.exports = { register, login, refresh, logout, deleteAllData };
+13
View File
@@ -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(); module.exports = new AuthService();