useGetIdentity is a hook that calls the getIdentity method from the authProvider under the hood. It returns a query result from @tanstack/react-query.
Usage
import { useGetIdentity } from "@refinedev/core";
const { data: user } = useGetIdentity();
console.log(user?.name);
Parameters
Options to configure the query from @tanstack/react-query.Enable or disable the query. By default, the query is enabled only if authProvider.getIdentity is defined.
queryOptions.retry
boolean | number
default:"false"
Retry configuration for failed queries.
Callback function to be called when the query succeeds.
Callback function to be called when the query fails.
Additional metadata to pass to the query.
Return Values
Returns a query result from @tanstack/react-query.
The user identity data returned from authProvider.getIdentity. The shape of this object depends on your authProvider implementation.
Indicates whether the query is loading.
Indicates whether the query is fetching.
Indicates whether the query was successful.
Indicates whether the query encountered an error.
Error object if the query failed.
A function to manually refetch the query.
Type Parameters
Type of the identity data returned from authProvider.getIdentity. Use this to type the user object.
Examples
Basic Usage
import { useGetIdentity } from "@refinedev/core";
interface IUser {
id: string;
name: string;
avatar: string;
}
const Header = () => {
const { data: user } = useGetIdentity<IUser>();
return (
<div>
<span>{user?.name}</span>
<img src={user?.avatar} alt={user?.name} />
</div>
);
};
import { useGetIdentity } from "@refinedev/core";
import { Avatar, Typography } from "antd";
interface IIdentity {
id: string;
name: string;
avatar: string;
}
const Header = () => {
const { data: user } = useGetIdentity<IIdentity>();
return (
<div style={{ display: "flex", alignItems: "center", gap: 16 }}>
<Typography.Text>{user?.name}</Typography.Text>
<Avatar src={user?.avatar} alt={user?.name} />
</div>
);
};
With Loading State
import { useGetIdentity } from "@refinedev/core";
interface IUser {
id: string;
name: string;
email: string;
}
const UserProfile = () => {
const { data: user, isLoading } = useGetIdentity<IUser>();
if (isLoading) {
return <div>Loading user data...</div>;
}
return (
<div>
<h2>{user?.name}</h2>
<p>{user?.email}</p>
</div>
);
};
Access Control Based on User
import { useGetIdentity, useOne } from "@refinedev/core";
interface Employee {
id: string;
name: string;
role: string;
department: string;
}
const TimeOffRequest = () => {
const { data: employee } = useGetIdentity<Employee>();
const { data: timeOffData } = useOne({
resource: "time-offs",
id: employee?.id,
});
if (employee?.role === "admin") {
return <AdminTimeOffView data={timeOffData} />;
}
return <EmployeeTimeOffView data={timeOffData} />;
};
Display Current User Properties
import { useGetIdentity } from "@refinedev/core";
interface User {
id: string;
email: string;
address: string;
}
const MyProfilePage = () => {
const { data: user } = useGetIdentity<User>();
return (
<div>
<h1>My Profile</h1>
<p>Email: {user?.email}</p>
<p>Address: {user?.address}</p>
</div>
);
};
Filter Data by Current User
import { useGetIdentity, useList } from "@refinedev/core";
interface User {
id: string;
name: string;
}
const MyProperties = () => {
const { data: user } = useGetIdentity<User>();
const { data: properties } = useList({
resource: "properties",
filters: [
{
field: "creator",
operator: "eq",
value: user?.id,
},
],
});
return (
<div>
<h2>My Properties</h2>
{properties?.data.map((property) => (
<PropertyCard key={property.id} property={property} />
))}
</div>
);
};
Web3 Authentication
import { useGetIdentity } from "@refinedev/core";
const Dashboard = () => {
const { data, isLoading } = useGetIdentity<{
address: string;
balance: string;
}>();
if (isLoading) {
return <div>Loading wallet info...</div>;
}
return (
<div>
<p>Wallet Address: {data?.address}</p>
<p>Balance: {data?.balance}</p>
</div>
);
};