mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 12:49:06 +00:00
authReady callback simplify
This commit is contained in:
@@ -1,53 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import useSessionStatus from "./useSessionStatus";
|
||||
import { isAuthConfigured } from "./apiClient";
|
||||
import { useAuth } from "./AuthProvider";
|
||||
|
||||
/**
|
||||
* Hook to check if authentication is fully ready.
|
||||
* This ensures both the session is authenticated AND the API client token is configured.
|
||||
* Prevents race conditions where React Query fires requests before the token is set.
|
||||
*/
|
||||
// TODO
|
||||
export default function useAuthReady() {
|
||||
const status = useSessionStatus();
|
||||
const isAuthenticated = status === "authenticated";
|
||||
const [authReady, setAuthReady] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let ready_ = false;
|
||||
// Check if both session is authenticated and token is configured
|
||||
const checkAuthReady = () => {
|
||||
const ready = isAuthenticated && isAuthConfigured();
|
||||
ready_ = ready;
|
||||
setAuthReady(ready);
|
||||
};
|
||||
|
||||
// Check immediately
|
||||
checkAuthReady();
|
||||
|
||||
// Also check periodically for a short time to catch async updates
|
||||
const interval = setInterval(checkAuthReady, 100);
|
||||
|
||||
// Stop checking after 2 seconds (auth should be ready by then)
|
||||
const timeout = setTimeout(() => {
|
||||
if (ready_) {
|
||||
clearInterval(interval);
|
||||
return;
|
||||
} else {
|
||||
console.warn("Auth not ready after 2 seconds");
|
||||
}
|
||||
}, 2000);
|
||||
|
||||
return () => {
|
||||
clearInterval(interval);
|
||||
clearTimeout(timeout);
|
||||
};
|
||||
}, [isAuthenticated]);
|
||||
const auth = useAuth();
|
||||
|
||||
return {
|
||||
isAuthReady: authReady,
|
||||
isLoading: status === "loading",
|
||||
isAuthenticated,
|
||||
isAuthenticated: auth.status === "authenticated",
|
||||
isLoading: auth.status === "loading",
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user