Skip to main content
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

queryOptions
UseQueryOptions
Options to configure the query from @tanstack/react-query.
queryOptions.enabled
boolean
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.
queryOptions.onSuccess
function
Callback function to be called when the query succeeds.
queryOptions.onError
function
Callback function to be called when the query fails.
queryOptions.meta
object
Additional metadata to pass to the query.

Return Values

Returns a query result from @tanstack/react-query.
data
TData
The user identity data returned from authProvider.getIdentity. The shape of this object depends on your authProvider implementation.
isLoading
boolean
Indicates whether the query is loading.
isFetching
boolean
Indicates whether the query is fetching.
isSuccess
boolean
Indicates whether the query was successful.
isError
boolean
Indicates whether the query encountered an error.
error
any
Error object if the query failed.
refetch
function
A function to manually refetch the query.

Type Parameters

TData
any
default:"any"
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>
  );
};

Display User in Header

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>
  );
};

Build docs developers (and LLMs) love