useAuthState
To avoid rendering a component, and to force waiting for the authProvider response, use useAuthState() instead of useAuthenticated(). It calls authProvider.checkAuth() on mount and returns an object with 2 properties:
isLoading:truejust after mount, while theauthProvideris being called.falseonce theauthProviderhas answered.authenticated:truewhile loading. thentrueorfalsedepending on theauthProviderresponse.
You can render different content depending on the authenticated status.
import { useAuthState, Loading } from 'react-admin';
const MyPage = () => {
const { isLoading, authenticated } = useAuthState();
if (isLoading) {
return <Loading />;
}
if (authenticated) {
return <AuthenticatedContent />;
}
return <AnonymousContent />;
};
