Skip to main content

useUser

The useUser hook provides access to the current user's profile and authentication state. It uses useFetch internally and supports SSR hydration.

import { useUser } from '@archibald/auth';

const { user, isLoggedIn, isLoading } = useUser<USER_TYPE>(OPTIONS);

Parameters

NameTypeRequiredDescription
optionsFetchMinOptionsOptions that define the behavior of the useFetch hook used internally.

Return value

  • Type: FetchResult<T>

The FetchResult object has the following properties:

PropertyTypeDescription
dataT | nullThe user profile data. Alias: user.
errorDefaultResponseError | nullError that occurred during fetching.
isLoadingbooleanIndicates if the user data is being loaded.
isLoggedInbooleanReturns true if the user is currently authenticated.
isDonebooleanReturns true when the data is finished loading.
refetchFunctionRe-execute the user data fetch.

Usage Example

import { useUser } from '@archibald/auth';

function Header() {
const { user, isLoggedIn, isLoading } = useUser();

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

return (
<header>
{isLoggedIn ? (
<span>Hello, {user.firstName}!</span>
) : (
<button>Login</button>
)}
</header>
);
}

Relates to

  • SessionClient: Uses sessionClient.loadUser() to retrieve user data.
  • useFetch: Internally calls @archibald/client's useFetch to handle caching and SSR hydration.
  • SessionClientProvider: Requires this provider to be present in the component tree to access the client instance.