Merge pull request #5 from okapteinis/claude/complete-session-fix-01WNkTGGrYURxe9JATA3gX6y

Fix complete session handling for 401 errors in all hooks
This commit is contained in:
Ojārs Kapteinis 2025-11-18 22:44:27 +02:00 committed by GitHub
commit e909e30392
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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",