mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
- Fix middleware management in apiClient to properly handle auth tokens - Update ApiAuthProvider to correctly configure base URL and auth - Add missing NextAuth API route handler at app/api/auth/[...nextauth]/route.ts - Remove middleware ejection attempts (not supported by openapi-fetch) - Use global variables to store current auth token and API URL - Setup middleware once on initialization instead of repeatedly adding This fixes the login/logout flow that was broken after migrating from the useApi compatibility layer to native React Query hooks.
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
"use client";
|
|
|
|
import createClient from "openapi-fetch";
|
|
import type { paths } from "../reflector-api";
|
|
import {
|
|
queryOptions,
|
|
useMutation,
|
|
useQuery,
|
|
useSuspenseQuery,
|
|
} from "@tanstack/react-query";
|
|
import createFetchClient from "openapi-react-query";
|
|
|
|
// Create the base openapi-fetch client
|
|
export const client = createClient<paths>({
|
|
// Base URL will be set dynamically via middleware
|
|
baseUrl: "",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
// Create the React Query client wrapper
|
|
export const $api = createFetchClient<paths>(client);
|
|
|
|
// Store the current auth token
|
|
let currentAuthToken: string | null | undefined = null;
|
|
|
|
// Set up authentication middleware once
|
|
client.use({
|
|
onRequest({ request }) {
|
|
if (currentAuthToken) {
|
|
request.headers.set("Authorization", `Bearer ${currentAuthToken}`);
|
|
}
|
|
return request;
|
|
},
|
|
});
|
|
|
|
// Configure authentication by updating the token
|
|
export const configureApiAuth = (token: string | null | undefined) => {
|
|
currentAuthToken = token;
|
|
};
|
|
|
|
// Export typed hooks for convenience
|
|
export const useApiQuery = $api.useQuery;
|
|
export const useApiMutation = $api.useMutation;
|
|
export const useApiSuspenseQuery = $api.useSuspenseQuery;
|