extended-salmon
extended-salmon6mo ago

GetIdentity is called twice

I am using classic authProvider with getIdentity method. Inside one of the components I use: const { data: user } = useGetIdentity<IIdentity | null>(); When I refresh application, getIdentity inside authProvider is called twice. If I comment out this line, getIdentity is still called once. Could it be that getIdentity is called somewhere in the background? If so, how could I access it's value inside my component so I don't have to make two requests?
2 Replies
kapa.ai
kapa.ai6mo ago
Hi @Tacho. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The getIdentity method is indeed called by the Refine framework itself. As per the Refine documentation (https://refine.dev/docs/guides-concepts/usage-with-existing-projects#optional-methods), the getIdentity method is used to enable the useGetIdentity hook, which is used for rendering current user information in the header of UI Integration layouts. This could be the reason why you see the getIdentity method being called even when you comment out your useGetIdentity hook. The framework itself is making a call to getIdentity to fetch user information for the header. To avoid making two requests, you can utilize the useGetIdentity hook provided by Refine. This hook calls the getIdentity method from the authProvider under the hood and returns the result of react-query's useQuery. The data resolved from the getIdentity will be returned as the data in the query result. Here's how you can use it:
import { useGetIdentity } from "@refinedev/core";

export const User = () => {
const { data: identity } = useGetIdentity<IIdentity>();

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

type IIdentity = {
id: number;
fullName: string;
};
import { useGetIdentity } from "@refinedev/core";

export const User = () => {
const { data: identity } = useGetIdentity<IIdentity>();

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

type IIdentity = {
id: number;
fullName: string;
};
In this example, identity will hold the data returned by your getIdentity method. You can use this data anywhere in your component without having to call getIdentity again. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6mo ago
<@537772056847187988> kapa.ai is still learning and improving, please let us know how it did by reacting below