Merge pull request #7 from okapteinis/claude/latvian-translations-01WNkTGGrYURxe9JATA3gX6y

Extract AUTH_TOKEN_KEY constant to improve maintainability
This commit is contained in:
Ojārs Kapteinis 2025-11-18 23:51:14 +02:00 committed by GitHub
commit 35f735dc6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 20 additions and 6 deletions

View file

@ -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",
}

View file

@ -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",
}

View file

@ -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",
});

View file

@ -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 || ""
);

View file

@ -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("");

View file

@ -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";