Spaces:
Running
Running
Merge pull request #200 from rishab11250/feature/hf-token-frontend
Browse files- frontend/src/lib/api.ts +23 -0
- frontend/src/store/auth-store.ts +7 -0
frontend/src/lib/api.ts
CHANGED
|
@@ -201,6 +201,29 @@ class ApiClient {
|
|
| 201 |
return res.json();
|
| 202 |
}
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
async postForm<T>(path: string, formData: FormData, options?: FetchOptions): Promise<T> {
|
| 205 |
const token = options?.token || this.getToken();
|
| 206 |
const headers: HeadersInit = {};
|
|
|
|
| 201 |
return res.json();
|
| 202 |
}
|
| 203 |
|
| 204 |
+
async put<T>(path: string, body?: unknown, options?: FetchOptions): Promise<T> {
|
| 205 |
+
const res = await this.fetchWithConnectionError(`${this.baseUrl}${path}`, {
|
| 206 |
+
method: "PUT",
|
| 207 |
+
headers: this.getHeaders(options?.token),
|
| 208 |
+
body: body ? JSON.stringify(body) : undefined,
|
| 209 |
+
...options,
|
| 210 |
+
});
|
| 211 |
+
|
| 212 |
+
// Auto-refresh on 401
|
| 213 |
+
if (res.status === 401 && !options?._skipRefresh) {
|
| 214 |
+
const newToken = await this.tryRefreshToken();
|
| 215 |
+
if (newToken) {
|
| 216 |
+
return this.put<T>(path, body, { ...options, token: newToken, _skipRefresh: true });
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
if (!res.ok) {
|
| 221 |
+
throw new Error(await this.getErrorMessage(res, res.statusText || "Request failed"));
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
return res.json();
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
async postForm<T>(path: string, formData: FormData, options?: FetchOptions): Promise<T> {
|
| 228 |
const token = options?.token || this.getToken();
|
| 229 |
const headers: HeadersInit = {};
|
frontend/src/store/auth-store.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface AuthUser {
|
|
| 8 |
username: string;
|
| 9 |
email: string;
|
| 10 |
is_admin: boolean;
|
|
|
|
| 11 |
created_at: string;
|
| 12 |
}
|
| 13 |
|
|
@@ -23,6 +24,7 @@ interface AuthStore {
|
|
| 23 |
initializeAuth: () => Promise<void>;
|
| 24 |
syncTokensRefreshed: (detail?: { accessToken?: string; user?: AuthUser | null }) => void;
|
| 25 |
syncLoggedOut: () => void;
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
const getStoredToken = () =>
|
|
@@ -138,4 +140,9 @@ export const useAuthStore = create<AuthStore>((set, get) => ({
|
|
| 138 |
initialized: true,
|
| 139 |
});
|
| 140 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
}));
|
|
|
|
| 8 |
username: string;
|
| 9 |
email: string;
|
| 10 |
is_admin: boolean;
|
| 11 |
+
hf_token?: string;
|
| 12 |
created_at: string;
|
| 13 |
}
|
| 14 |
|
|
|
|
| 24 |
initializeAuth: () => Promise<void>;
|
| 25 |
syncTokensRefreshed: (detail?: { accessToken?: string; user?: AuthUser | null }) => void;
|
| 26 |
syncLoggedOut: () => void;
|
| 27 |
+
setHfToken: (hfToken: string) => Promise<void>;
|
| 28 |
}
|
| 29 |
|
| 30 |
const getStoredToken = () =>
|
|
|
|
| 140 |
initialized: true,
|
| 141 |
});
|
| 142 |
},
|
| 143 |
+
|
| 144 |
+
async setHfToken(hfToken: string) {
|
| 145 |
+
const response = await api.put<AuthUser>("/api/v1/auth/hf-token", { hf_token: hfToken });
|
| 146 |
+
set({ user: response });
|
| 147 |
+
},
|
| 148 |
}));
|