Skip to main content
useIsAuthenticated is a hook that calls the check method from the authProvider under the hood. It returns a query result from @tanstack/react-query.

Usage

import { useIsAuthenticated } from "@refinedev/core";

const { data, isLoading } = useIsAuthenticated();

if (isLoading) {
  return <div>Loading...</div>;
}

if (data?.authenticated) {
  return <div>User is authenticated</div>;
}

Parameters

queryOptions
UseQueryOptions
Options to configure the query from @tanstack/react-query.
queryOptions.enabled
boolean
Enable or disable the query.
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.
params
any
Additional parameters to pass to the authProvider.check method.

Return Values

Returns a query result from @tanstack/react-query.
data
CheckResponse
The authentication check response.
data.authenticated
boolean
Indicates whether the user is authenticated.
data.redirectTo
string
The URL to redirect to if not authenticated.
data.logout
boolean
Indicates whether the user should be logged out.
data.error
Error | RefineError
Error object if authentication check failed.
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.

Examples

Basic Usage

import { useIsAuthenticated } from "@refinedev/core";

const ProtectedComponent = () => {
  const { data, isLoading } = useIsAuthenticated();

  if (isLoading) {
    return <div>Checking authentication...</div>;
  }

  if (!data?.authenticated) {
    return <div>Please log in to access this content</div>;
  }

  return <div>Protected content</div>;
};

Conditional Rendering

import { useIsAuthenticated } from "@refinedev/core";
import { LoginButton } from "./LoginButton";
import { UserProfile } from "./UserProfile";

const Header = () => {
  const { data } = useIsAuthenticated();

  return (
    <header>
      {data?.authenticated ? <UserProfile /> : <LoginButton />}
    </header>
  );
};

With Custom Parameters

import { useIsAuthenticated } from "@refinedev/core";

const { data } = useIsAuthenticated({
  params: {
    resource: "posts",
    action: "edit",
  },
});

Disable Auto-Check

import { useIsAuthenticated } from "@refinedev/core";
import { useState } from "react";

const ManualAuthCheck = () => {
  const [checkAuth, setCheckAuth] = useState(false);

  const { data, refetch } = useIsAuthenticated({
    queryOptions: {
      enabled: checkAuth,
    },
  });

  return (
    <div>
      <button onClick={() => refetch()}>Check Authentication</button>
      {data?.authenticated ? "Authenticated" : "Not authenticated"}
    </div>
  );
};

In a Canvas Application

import { useIsAuthenticated } from "@refinedev/core";

const CanvasHeader = () => {
  const { data } = useIsAuthenticated();
  const isAuthenticated = data?.authenticated;

  return (
    <header>
      <nav>
        <Link to="/">Home</Link>
        <Link to="/canvases">Canvases</Link>
      </nav>
      {isAuthenticated ? (
        <LogoutButton />
      ) : (
        <LoginButton />
      )}
    </header>
  );
};

Build docs developers (and LLMs) love