diff --git a/apps/ops/src/api/auth.js b/apps/ops/src/api/auth.js index d202d40..33c797b 100644 --- a/apps/ops/src/api/auth.js +++ b/apps/ops/src/api/auth.js @@ -4,22 +4,42 @@ import { BASE_URL } from 'src/config/erpnext' // Only needed for local dev (VITE_ERP_TOKEN in .env). const SERVICE_TOKEN = import.meta.env.VITE_ERP_TOKEN || '' -export function authFetch (url, opts = {}) { - if (SERVICE_TOKEN) { - opts.headers = { - ...opts.headers, - Authorization: 'token ' + SERVICE_TOKEN, - } - } else { - opts.headers = { ...opts.headers } +// Reconnexion auto sur session Authentik expirée, anti-boucle (1 reload / 20 s max). +let _lastReload = 0 +function _reauth (status) { + const now = Date.now() + if (now - _lastReload > 20000) { + _lastReload = now + window.location.reload() } - return fetch(url, opts).then(res => { - if (res.status === 401 || res.status === 403) { - window.location.reload() - return new Response('{}', { status: res.status }) - } - return res - }) + return new Response('{}', { status: status || 401 }) +} + +export async function authFetch (url, opts = {}) { + opts.headers = SERVICE_TOKEN + ? { ...opts.headers, Authorization: 'token ' + SERVICE_TOKEN } + : { ...opts.headers } + + let res + try { + res = await fetch(url, opts) + } catch (e) { + // Coupure réseau transitoire (ex: backend qui redémarre) → 1 retry court avant d'abandonner + await new Promise(r => setTimeout(r, 800)) + res = await fetch(url, opts) + } + + // Détection « session Authentik expirée » (la cause du "il faut recharger") : + // - 401/403 explicites + // - réponse redirigée vers l'IdP (auth.targo.ca / /if/flow/) + // - page HTML de login renvoyée à la place du JSON attendu sur un /api/ + const ct = (res.headers && res.headers.get && res.headers.get('content-type')) || '' + const redirectedToIdp = res.redirected && /auth\.targo\.ca|\/if\/flow\//.test(res.url || '') + const htmlForApi = res.status === 200 && ct.includes('text/html') && /\/api\//.test(String(url)) + if (res.status === 401 || res.status === 403 || redirectedToIdp || htmlForApi) { + return _reauth(res.status) + } + return res } export async function getLoggedUser () {