From 30aa44bb913d68609262447ce3fbb9081397cf37 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Fri, 26 Dec 2025 23:03:19 +0530 Subject: [PATCH 1/2] refactor: update BaseApiService to use dynamic bearer token retrieval - Replaced static bearerToken property with a getter that retrieves the token from localStorage. - Updated constructor to remove bearerToken parameter for cleaner initialization. - Maintained setBearerToken method for backward compatibility, now a no-op. --- surfsense_web/lib/apis/base-api.service.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/surfsense_web/lib/apis/base-api.service.ts b/surfsense_web/lib/apis/base-api.service.ts index 0f3c17d4e..1a2921b5d 100644 --- a/surfsense_web/lib/apis/base-api.service.ts +++ b/surfsense_web/lib/apis/base-api.service.ts @@ -21,18 +21,23 @@ export type RequestOptions = { }; class BaseApiService { - bearerToken: string; baseUrl: string; noAuthEndpoints: string[] = ["/auth/jwt/login", "/auth/register", "/auth/refresh"]; // Add more endpoints as needed - constructor(bearerToken: string, baseUrl: string) { - this.bearerToken = bearerToken; + // Use a getter to always read fresh token from localStorage + // This ensures the token is always up-to-date after login/logout + get bearerToken(): string { + return typeof window !== "undefined" ? getBearerToken() || "" : ""; + } + + constructor(baseUrl: string) { this.baseUrl = baseUrl; } - setBearerToken(bearerToken: string) { - this.bearerToken = bearerToken; + // Keep for backward compatibility, but token is now always read from localStorage + setBearerToken(_bearerToken: string) { + // No-op: token is now always read fresh from localStorage via the getter } async request( @@ -294,6 +299,5 @@ class BaseApiService { } export const baseApiService = new BaseApiService( - typeof window !== "undefined" ? getBearerToken() || "" : "", process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "" ); From 7e7030ef46fdc9c9e4906c1b60051a0233ca2e7b Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Sat, 27 Dec 2025 00:03:03 +0530 Subject: [PATCH 2/2] chore: fixed linting --- surfsense_web/lib/apis/base-api.service.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/surfsense_web/lib/apis/base-api.service.ts b/surfsense_web/lib/apis/base-api.service.ts index 1a2921b5d..ff71fe14c 100644 --- a/surfsense_web/lib/apis/base-api.service.ts +++ b/surfsense_web/lib/apis/base-api.service.ts @@ -298,6 +298,4 @@ class BaseApiService { } } -export const baseApiService = new BaseApiService( - process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "" -); +export const baseApiService = new BaseApiService(process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "");