useAuthProvider

React-admin stores the authProvider object in a React context, so it’s available from anywhere in your application code. The useAuthProvider hook reads this context to let you call the authProvider directly.

For instance, here is how to call the Auth Provider to get the identity of the current logged-in user:

import { useState, useEffect } from 'react';
import { useAuthProvider } from 'react-admin';

import { Loading, Error } from './MyComponents';

const UserName = ({ userId }) => {
    const authProvider = useAuthProvider();
    const [identity, setIdentity] = useState();
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState();
    useEffect(() => {
        authProvider.getIdentity()
            .then(({ data }) => {
                setIdentity(data);
                setLoading(false);
            })
            .catch(error => {
                setError(error);
                setLoading(false);
            })
    }, [authProvider]);

    if (loading) return <Loading />;
    if (error) return <Error />;
    if (!identity) return null;

    return <>{identity.fullName}</>;
};

But the recommended way to query the Data Provider is to use the authProvider method hooks (like useGetIdentity for instance). Using these hooks, you don’t have to handle the call state yourself.

import { useState, useEffect } from 'react';
import { useGetIdentity } from 'react-admin';

import { Loading, Error } from './MyComponents';

const UserName = ({ userId }) => {
    const { identity, isLoading, error } = useGetIdentity();

    if (isLoading) return <Loading />;
    if (error) return <Error />;
    if (!identity) return null;

    return <>{identity.fullName}</>;
};