Fix complete session handling for 401 errors in all hooks

- Add baseApiService token clearing on 401 in use-user.ts
- Add baseApiService token clearing on 401 in use-chats.ts (both fetchChats and deleteChat)
- Add baseApiService token clearing on 401 in use-search-space.ts
- Update all redirects to /login?error=session_expired for better UX
- Add session_expired error message to auth-errors.ts

This ensures consistent session invalidation across all API hooks,
preventing stale auth state in the baseApiService singleton.
This commit is contained in:
Claude 2025-11-18 20:30:37 +00:00
parent fa8a4b94c8
commit f8fc3fa9c4
No known key found for this signature in database
4 changed files with 22 additions and 8 deletions

View file

@ -2,6 +2,7 @@
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner";
import { baseApiService } from "@/lib/apis/base-api.service";
interface Chat {
created_at: string;
@ -46,9 +47,10 @@ export function useChats({
);
if (response.status === 401) {
// Clear token and redirect to home
// Clear token from both localStorage and baseApiService
localStorage.removeItem("surfsense_bearer_token");
window.location.href = "/";
baseApiService.setBearerToken("");
window.location.href = "/login?error=session_expired";
throw new Error("Unauthorized: Redirecting to login page");
}
@ -90,9 +92,10 @@ export function useChats({
);
if (response.status === 401) {
// Clear token and redirect to home
// Clear token from both localStorage and baseApiService
localStorage.removeItem("surfsense_bearer_token");
window.location.href = "/";
baseApiService.setBearerToken("");
window.location.href = "/login?error=session_expired";
throw new Error("Unauthorized: Redirecting to login page");
}

View file

@ -2,6 +2,7 @@
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner";
import { baseApiService } from "@/lib/apis/base-api.service";
interface SearchSpace {
created_at: string;
@ -38,9 +39,10 @@ export function useSearchSpace({ searchSpaceId, autoFetch = true }: UseSearchSpa
);
if (response.status === 401) {
// Clear token and redirect to home
// Clear token from both localStorage and baseApiService
localStorage.removeItem("surfsense_bearer_token");
window.location.href = "/";
baseApiService.setBearerToken("");
window.location.href = "/login?error=session_expired";
throw new Error("Unauthorized: Redirecting to login page");
}

View file

@ -2,6 +2,7 @@
import { useEffect, useState } from "react";
import { toast } from "sonner";
import { baseApiService } from "@/lib/apis/base-api.service";
interface User {
id: string;
@ -33,9 +34,11 @@ export function useUser() {
});
if (response.status === 401) {
// Clear token and redirect to home
// Clear token from both localStorage and baseApiService
localStorage.removeItem("surfsense_bearer_token");
window.location.href = "/";
baseApiService.setBearerToken("");
// Redirect to login with session expired message
window.location.href = "/login?error=session_expired";
throw new Error("Unauthorized: Redirecting to login page");
}

View file

@ -92,6 +92,12 @@ const AUTH_ERROR_MESSAGES: AuthErrorMapping = {
description: "Login is temporarily unavailable. Please try again later",
},
// Session errors
session_expired: {
title: "Session expired",
description: "Your session has expired. Please sign in again",
},
// Network errors
NETWORK_ERROR: {
title: "Connection failed",