fix: recover gracefully from stale sessions causing repeated 401s

The SPA's session check (/api/auth/session) is cached indefinitely by
react-query (staleTime: Infinity, no refetch on focus/interval), so once a
protected page mounts successfully, the app never re-verifies auth. If the
server-side session later becomes invalid (server restart with in-memory
sessions, cookie/session expiry, etc.), every subsequent API call
(pages, scheduled-posts, audio-tracks, media, media/upload, ...) starts
failing with 401 in a loop with no way for the user to recover short of a
manual page reload.

Add a shared handleUnauthorized() in queryClient.ts that redirects to
/login on any non-auth API 401, wired into both the shared fetch helpers
(apiRequest/getQueryFn) and the raw fetch() calls used for file uploads
and Remotion rendering, which also lacked this recovery path. Also add the
missing credentials: "include" to those raw fetch() calls for consistency
with the rest of the app's API requests.
This commit is contained in:
Claude committed 2026-07-27 11:56:56 +00:00
1 parent 75e1d88c26
commit 2188fa458d
9 files changed
+81 -24

No files matched your search

+5
View File
@@ -8,6 +8,7 @@ import { useToast } from "@/hooks/use-toast";
import { Trash2, Music, Upload, Loader2, Play, Pause } from "lucide-react";
import { format } from "date-fns";
import { fr } from "date-fns/locale";
import { handleUnauthorized } from "@/lib/queryClient";
interface AudioTrack {
id: number;
@@ -41,9 +42,11 @@ export default function AudioAdmin() {
const res = await fetch("/api/audio-tracks", {
method: "POST",
body: formData,
credentials: "include",
});
if (!res.ok) {
if (res.status === 401) handleUnauthorized("/api/audio-tracks");
const error = await res.json();
throw new Error(error.error || "Erreur lors de l'upload");
}
@@ -77,9 +80,11 @@ export default function AudioAdmin() {
mutationFn: async (id: number) => {
const res = await fetch(`/api/audio-tracks/${id}`, {
method: "DELETE",
credentials: "include",
});
if (!res.ok) {
if (res.status === 401) handleUnauthorized(`/api/audio-tracks/${id}`);
throw new Error("Erreur lors de la suppression");
}
},