Add getSessionsInfo method to retrieve user session details
Despliegue Automático / desplegar (push) Successful in 1m24s
Despliegue Automático / desplegar (push) Successful in 1m24s
This commit is contained in:
@@ -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 };
|
||||||
@@ -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();
|
||||||
Reference in New Issue
Block a user