Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions frontend/components/ToastProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,28 @@ export function ToastProvider({ children }: { children: ReactNode }) {

// createPortal needs `document`, which doesn't exist during SSR — mirrors
// the mounted-guard pattern already used in ThemeToggle.tsx.
useEffect(() => setMounted(true), []);
useEffect(() => setMounted(true), []);

const dismiss = useCallback((id: string) => {
setToasts((prev) => prev.filter((t) => t.id !== id));
}, []);

const push = useCallback(
const push = useCallback(
(type: ToastType, message: string, duration = DEFAULT_DURATION_MS) => {
const id = `toast-${nextId.current++}`;
setToasts((prev) =>
[...prev, { id, type, message, duration }].slice(-MAX_TOASTS),
);
},
[],
);
[],
);

useEffect(() => {
const onSessionExpired = () =>
push("error", "Your session has expired — please log in again", 0);
window.addEventListener("airflex:session-expired", onSessionExpired);
return () => window.removeEventListener("airflex:session-expired", onSessionExpired);
}, [push]);

const value = useMemo<ToastContextValue>(
() => ({
Expand Down Expand Up @@ -143,4 +150,4 @@ export function ToastProvider({ children }: { children: ReactNode }) {
)}
</ToastContext.Provider>
);
}
}
2 changes: 1 addition & 1 deletion frontend/lib/apiFetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ describe("apiFetch authentication", () => {
const [, options] = mockFetch.mock.calls[0] as [string, RequestInit];
expect(options.headers).not.toHaveProperty("Authorization");
});
});
});
12 changes: 11 additions & 1 deletion frontend/lib/apiFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* apiFetch.ts — Centralized API fetch wrapper and error type for frontend.
*/

import { getToken } from "../app/lib/auth";
import { clearToken, getToken } from "../app/lib/auth";

export class ApiError extends Error {
status: number;
Expand Down Expand Up @@ -63,6 +63,16 @@ export async function apiFetch<T>(
// response was not JSON
}

if (response.status === 401 && typeof window !== "undefined") {
clearToken();
window.dispatchEvent(new CustomEvent("airflex:session-expired"));

const returnTo = `${window.location.pathname}${window.location.search}${window.location.hash}`;
if (!window.location.pathname.startsWith("/auth/signup")) {
window.location.assign(`/auth/signup?returnTo=${encodeURIComponent(returnTo)}`);
}
}

throw new ApiError(errorMessage, response.status, errorData);
}

Expand Down
Loading