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
:true
just after mount, while theauthProvider
is being called.false
once theauthProvider
has answered.authenticated
:true
while loading. thentrue
orfalse
depending on theauthProvider
response.
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 />;
};