mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 08:46:22 +02:00
refactor: centralize authentication handling
- Replaced direct localStorage token access with a centralized `getBearerToken` function across various components and hooks to improve code maintainability and security. - Updated API calls to use `authenticatedFetch` for consistent authentication handling. - Enhanced user experience by ensuring proper redirection to login when authentication fails. - Cleaned up unused imports and improved overall code structure for better readability.
This commit is contained in:
parent
6cc9e38e1d
commit
b2a97b39ce
35 changed files with 396 additions and 497 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { th } from "date-fns/locale";
|
||||
import type z from "zod";
|
||||
import { getBearerToken, handleUnauthorized } from "../auth-utils";
|
||||
import { AppError, AuthenticationError, AuthorizationError, NotFoundError } from "../error";
|
||||
|
||||
enum ResponseType {
|
||||
|
|
@ -132,6 +132,8 @@ class BaseApiService {
|
|||
|
||||
switch (response.status) {
|
||||
case 401:
|
||||
// Use centralized auth handler for 401 responses
|
||||
handleUnauthorized();
|
||||
throw new AuthenticationError(
|
||||
"You are not authenticated. Please login again.",
|
||||
response.status,
|
||||
|
|
@ -261,6 +263,6 @@ class BaseApiService {
|
|||
}
|
||||
|
||||
export const baseApiService = new BaseApiService(
|
||||
typeof window !== "undefined" ? localStorage.getItem("surfsense_bearer_token") || "" : "",
|
||||
typeof window !== "undefined" ? getBearerToken() || "" : "",
|
||||
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue