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
| Name | Type | Required | Description |
|---|---|---|---|
| options | FetchMinOptions | Options that define the behavior of the useFetch hook used internally. |
Return value
- Type:
FetchResult<T>
The FetchResult object has the following properties:
| Property | Type | Description |
|---|---|---|
| data | T | null | The user profile data. Alias: user. |
| error | DefaultResponseError | null | Error that occurred during fetching. |
| isLoading | boolean | Indicates if the user data is being loaded. |
| isLoggedIn | boolean | Returns true if the user is currently authenticated. |
| isDone | boolean | Returns true when the data is finished loading. |
| refetch | Function | Re-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: UsessessionClient.loadUser()to retrieve user data.useFetch: Internally calls@archibald/client'suseFetchto handle caching and SSR hydration.SessionClientProvider: Requires this provider to be present in the component tree to access the client instance.