mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
fix: authentication flow with React Query migration
- 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.
This commit is contained in:
@@ -22,16 +22,22 @@ export const client = createClient<paths>({
|
||||
// Create the React Query client wrapper
|
||||
export const $api = createFetchClient<paths>(client);
|
||||
|
||||
// Configure authentication
|
||||
// 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) => {
|
||||
if (token) {
|
||||
client.use({
|
||||
onRequest({ request }) {
|
||||
request.headers.set("Authorization", `Bearer ${token}`);
|
||||
return request;
|
||||
},
|
||||
});
|
||||
}
|
||||
currentAuthToken = token;
|
||||
};
|
||||
|
||||
// Export typed hooks for convenience
|
||||
|
||||
Reference in New Issue
Block a user