From 1a83d6a3ef24439e282d40ee81e3e35baf48e0de Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 18 Nov 2025 21:49:56 +0000 Subject: [PATCH] Extract AUTH_TOKEN_KEY constant to improve maintainability - Create lib/constants.ts with AUTH_TOKEN_KEY - Update auth-utils.ts to use constant - Update use-user.ts, use-chats.ts, use-search-space.ts - Update base-api.service.ts This prevents typos and makes future updates simpler across the application per PR review feedback. --- surfsense_web/hooks/use-chats.ts | 5 +++-- surfsense_web/hooks/use-search-space.ts | 3 ++- surfsense_web/hooks/use-user.ts | 3 ++- surfsense_web/lib/apis/base-api.service.ts | 3 ++- surfsense_web/lib/auth-utils.ts | 3 ++- surfsense_web/lib/constants.ts | 9 +++++++++ 6 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 surfsense_web/lib/constants.ts diff --git a/surfsense_web/hooks/use-chats.ts b/surfsense_web/hooks/use-chats.ts index efb2dd2e1..e8f66145b 100644 --- a/surfsense_web/hooks/use-chats.ts +++ b/surfsense_web/hooks/use-chats.ts @@ -3,6 +3,7 @@ import { useCallback, useEffect, useState } from "react"; import { toast } from "sonner"; import { handleSessionExpired } from "@/lib/auth-utils"; +import { AUTH_TOKEN_KEY } from "@/lib/constants"; interface Chat { created_at: string; @@ -40,7 +41,7 @@ export function useChats({ `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats?limit=${limit}&skip=${skip}&search_space_id=${searchSpaceId}`, { headers: { - Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`, + Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`, }, method: "GET", } @@ -81,7 +82,7 @@ export function useChats({ `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${chatId}`, { headers: { - Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`, + Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`, }, method: "DELETE", } diff --git a/surfsense_web/hooks/use-search-space.ts b/surfsense_web/hooks/use-search-space.ts index 45fb7bb62..aa0609fa8 100644 --- a/surfsense_web/hooks/use-search-space.ts +++ b/surfsense_web/hooks/use-search-space.ts @@ -3,6 +3,7 @@ import { useCallback, useEffect, useState } from "react"; import { toast } from "sonner"; import { handleSessionExpired } from "@/lib/auth-utils"; +import { AUTH_TOKEN_KEY } from "@/lib/constants"; interface SearchSpace { created_at: string; @@ -32,7 +33,7 @@ export function useSearchSpace({ searchSpaceId, autoFetch = true }: UseSearchSpa `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${searchSpaceId}`, { headers: { - Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`, + Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`, }, method: "GET", } diff --git a/surfsense_web/hooks/use-user.ts b/surfsense_web/hooks/use-user.ts index 578a90e08..6a3062cf6 100644 --- a/surfsense_web/hooks/use-user.ts +++ b/surfsense_web/hooks/use-user.ts @@ -3,6 +3,7 @@ import { useEffect, useState } from "react"; import { toast } from "sonner"; import { handleSessionExpired } from "@/lib/auth-utils"; +import { AUTH_TOKEN_KEY } from "@/lib/constants"; interface User { id: string; @@ -28,7 +29,7 @@ export function useUser() { setLoading(true); const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/users/me`, { headers: { - Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`, + Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`, }, method: "GET", }); diff --git a/surfsense_web/lib/apis/base-api.service.ts b/surfsense_web/lib/apis/base-api.service.ts index ea7b1cb7d..b37dc69c6 100644 --- a/surfsense_web/lib/apis/base-api.service.ts +++ b/surfsense_web/lib/apis/base-api.service.ts @@ -6,6 +6,7 @@ import { NotFoundError, ValidationError, } from "../error"; +import { AUTH_TOKEN_KEY } from "../constants"; export type RequestOptions = { method: "GET" | "POST" | "PUT" | "DELETE"; @@ -182,6 +183,6 @@ export class BaseApiService { } export const baseApiService = new BaseApiService( - typeof window !== "undefined" ? localStorage.getItem("surfsense_bearer_token") || "" : "", + typeof window !== "undefined" ? localStorage.getItem(AUTH_TOKEN_KEY) || "" : "", process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "" ); diff --git a/surfsense_web/lib/auth-utils.ts b/surfsense_web/lib/auth-utils.ts index 3ebb5c332..76f1716da 100644 --- a/surfsense_web/lib/auth-utils.ts +++ b/surfsense_web/lib/auth-utils.ts @@ -3,6 +3,7 @@ */ import { baseApiService } from "./apis/base-api.service"; +import { AUTH_TOKEN_KEY } from "./constants"; /** * Handle session expiration by clearing tokens and redirecting to login @@ -10,7 +11,7 @@ import { baseApiService } from "./apis/base-api.service"; */ export function handleSessionExpired(): never { // Clear token from localStorage - localStorage.removeItem("surfsense_bearer_token"); + localStorage.removeItem(AUTH_TOKEN_KEY); // Clear token from baseApiService singleton to prevent stale auth state baseApiService.setBearerToken(""); diff --git a/surfsense_web/lib/constants.ts b/surfsense_web/lib/constants.ts new file mode 100644 index 000000000..348ef5cbf --- /dev/null +++ b/surfsense_web/lib/constants.ts @@ -0,0 +1,9 @@ +/** + * Application-wide constants + */ + +/** + * Local storage key for the authentication bearer token + * Used across auth-utils, hooks, and base-api.service + */ +export const AUTH_TOKEN_KEY = "surfsense_bearer_token";