fix: change expired JWT response code to 401 to properly trigger frontend interceptor
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m42s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 1m42s
- The backend was returning 403 Forbidden when a token expired, causing the frontend apiFetch interceptor (which listens for 401) to ignore it and crash the session.
This commit is contained in:
@@ -137,7 +137,7 @@ const authenticateToken = async (req, res, next) => {
|
|||||||
if (!token) return res.status(401).json({ error: 'Token não fornecido.' });
|
if (!token) return res.status(401).json({ error: 'Token não fornecido.' });
|
||||||
|
|
||||||
jwt.verify(token, JWT_SECRET, (err, user) => {
|
jwt.verify(token, JWT_SECRET, (err, user) => {
|
||||||
if (err) return res.status(403).json({ error: 'Token inválido ou expirado.' });
|
if (err) return res.status(401).json({ error: 'Token inválido ou expirado.' });
|
||||||
req.user = user;
|
req.user = user;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
@@ -1154,8 +1154,19 @@ apiRouter.get('/integration/users', requireRole(['admin']), async (req, res) =>
|
|||||||
apiRouter.get('/integration/origins', requireRole(['admin']), async (req, res) => {
|
apiRouter.get('/integration/origins', requireRole(['admin']), async (req, res) => {
|
||||||
if (!req.user.is_api_key) return res.status(403).json({ error: 'Endpoint restrito a chaves de API.' });
|
if (!req.user.is_api_key) return res.status(403).json({ error: 'Endpoint restrito a chaves de API.' });
|
||||||
try {
|
try {
|
||||||
const [origins] = await pool.query('SELECT name FROM origins WHERE tenant_id = ? ORDER BY created_at ASC', [req.user.tenant_id]);
|
const [groups] = await pool.query('SELECT id, name FROM origin_groups WHERE tenant_id = ?', [req.user.tenant_id]);
|
||||||
res.json(origins.map(o => o.name));
|
if (groups.length === 0) return res.json([]);
|
||||||
|
|
||||||
|
const [items] = await pool.query('SELECT origin_group_id, name FROM origin_items WHERE origin_group_id IN (?) ORDER BY created_at ASC', [groups.map(g => g.id)]);
|
||||||
|
const [teams] = await pool.query('SELECT id as team_id, name as team_name, origin_group_id FROM teams WHERE tenant_id = ? AND origin_group_id IS NOT NULL', [req.user.tenant_id]);
|
||||||
|
|
||||||
|
const result = groups.map(g => ({
|
||||||
|
group_name: g.name,
|
||||||
|
origins: items.filter(i => i.origin_group_id === g.id).map(i => i.name),
|
||||||
|
assigned_teams: teams.filter(t => t.origin_group_id === g.id).map(t => ({ id: t.team_id, name: t.team_name }))
|
||||||
|
}));
|
||||||
|
|
||||||
|
res.json(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user