mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
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.
This commit is contained in:
parent
2f5d1b6a9b
commit
30aa44bb91
1 changed files with 10 additions and 6 deletions
|
|
@ -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<T, R extends ResponseType = ResponseType.JSON>(
|
||||
|
|
@ -294,6 +299,5 @@ class BaseApiService {
|
|||
}
|
||||
|
||||
export const baseApiService = new BaseApiService(
|
||||
typeof window !== "undefined" ? getBearerToken() || "" : "",
|
||||
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue