mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-24 23:41:10 +02:00
Merge pull request #7 from okapteinis/claude/latvian-translations-01WNkTGGrYURxe9JATA3gX6y
Extract AUTH_TOKEN_KEY constant to improve maintainability
This commit is contained in:
commit
35f735dc6a
6 changed files with 20 additions and 6 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { handleSessionExpired } from "@/lib/auth-utils";
|
import { handleSessionExpired } from "@/lib/auth-utils";
|
||||||
|
import { AUTH_TOKEN_KEY } from "@/lib/constants";
|
||||||
|
|
||||||
interface Chat {
|
interface Chat {
|
||||||
created_at: string;
|
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}`,
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats?limit=${limit}&skip=${skip}&search_space_id=${searchSpaceId}`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
|
Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`,
|
||||||
},
|
},
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
}
|
||||||
|
|
@ -81,7 +82,7 @@ export function useChats({
|
||||||
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${chatId}`,
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/chats/${chatId}`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
|
Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`,
|
||||||
},
|
},
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { handleSessionExpired } from "@/lib/auth-utils";
|
import { handleSessionExpired } from "@/lib/auth-utils";
|
||||||
|
import { AUTH_TOKEN_KEY } from "@/lib/constants";
|
||||||
|
|
||||||
interface SearchSpace {
|
interface SearchSpace {
|
||||||
created_at: string;
|
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}`,
|
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/searchspaces/${searchSpaceId}`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
|
Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`,
|
||||||
},
|
},
|
||||||
method: "GET",
|
method: "GET",
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { handleSessionExpired } from "@/lib/auth-utils";
|
import { handleSessionExpired } from "@/lib/auth-utils";
|
||||||
|
import { AUTH_TOKEN_KEY } from "@/lib/constants";
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
@ -28,7 +29,7 @@ export function useUser() {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/users/me`, {
|
const response = await fetch(`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/users/me`, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${localStorage.getItem("surfsense_bearer_token")}`,
|
Authorization: `Bearer ${localStorage.getItem(AUTH_TOKEN_KEY)}`,
|
||||||
},
|
},
|
||||||
method: "GET",
|
method: "GET",
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import {
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
ValidationError,
|
ValidationError,
|
||||||
} from "../error";
|
} from "../error";
|
||||||
|
import { AUTH_TOKEN_KEY } from "../constants";
|
||||||
|
|
||||||
export type RequestOptions = {
|
export type RequestOptions = {
|
||||||
method: "GET" | "POST" | "PUT" | "DELETE";
|
method: "GET" | "POST" | "PUT" | "DELETE";
|
||||||
|
|
@ -182,6 +183,6 @@ export class BaseApiService {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const baseApiService = new 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 || ""
|
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { baseApiService } from "./apis/base-api.service";
|
import { baseApiService } from "./apis/base-api.service";
|
||||||
|
import { AUTH_TOKEN_KEY } from "./constants";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle session expiration by clearing tokens and redirecting to login
|
* 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 {
|
export function handleSessionExpired(): never {
|
||||||
// Clear token from localStorage
|
// Clear token from localStorage
|
||||||
localStorage.removeItem("surfsense_bearer_token");
|
localStorage.removeItem(AUTH_TOKEN_KEY);
|
||||||
|
|
||||||
// Clear token from baseApiService singleton to prevent stale auth state
|
// Clear token from baseApiService singleton to prevent stale auth state
|
||||||
baseApiService.setBearerToken("");
|
baseApiService.setBearerToken("");
|
||||||
|
|
|
||||||
9
surfsense_web/lib/constants.ts
Normal file
9
surfsense_web/lib/constants.ts
Normal 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";
|
||||||
Loading…
Add table
Add a link
Reference in a new issue