The useAuth() hook provides access to the current user's authentication state and methods to manage the active session. You can use this hook to get the user's authentication and organization information, like their ID, session ID, session token, organization ID, and organization role, and to check if the user is authenticated or authorized to access a specific resource.
An object containing the initial authentication state or options for the useAuth() hook. If not provided, the hook will attempt to derive the state from the context. treatPendingAsSignedOut is a boolean that indicates whether pending sessions are considered as signed out or not. Defaults to true.
A function that retrieves the current user's session token or a custom JWT template. Returns a promise that resolves to the token. See the reference docJavaScript Icon.
Name
has
Type
undefined
Description
A function that checks if the user has specific Permissions or Roles. See the reference docClerk Icon.
Name
isLoaded
Type
false
Description
A boolean that indicates whether Clerk has completed initialization. Initially false, becomes true once Clerk loads.
Name
isSignedIn
Type
undefined
Description
A boolean that indicates whether a user is currently signed in.
Name
orgId
Type
undefined
Description
The ID of the user's active Organization.
Name
orgRole
Type
undefined
Description
The current user's Role in their active Organization.
Name
orgSlug
Type
undefined
Description
The URL-friendly identifier of the user's Active Organization.
A function that retrieves the current user's session token or a custom JWT template. Returns a promise that resolves to the token. See the reference docJavaScript Icon.
A function that retrieves the current user's session token or a custom JWT template. Returns a promise that resolves to the token. See the reference docJavaScript Icon.
A function that retrieves the current user's session token or a custom JWT template. Returns a promise that resolves to the token. See the reference docJavaScript Icon.
The following example demonstrates how to use the useAuth() hook to access the current auth state, like whether the user is signed in or not. It also includes a basic example for using the getToken() method to retrieve a session token for fetching data from an external resource.
app/routes/index.tsx
import { useAuth } from'@clerk/tanstack-react-start'import { createFileRoute } from'@tanstack/react-router'exportconstRoute=createFileRoute('/')({ component: ExternalDataPage,})functionExternalDataPage() {const { userId,sessionId,getToken,isLoaded,isSignedIn } =useAuth()constfetchExternalData=async () => {consttoken=awaitgetToken()// Fetch data from an external APIconstresponse=awaitfetch('https://api.example.com/data', { headers: { Authorization:`Bearer ${token}`, }, })returnresponse.json() }// Handle loading stateif (!isLoaded) return <div>Loading...</div>// Protect the page from unauthenticated usersif (!isSignedIn) return <div>Sign in to view this page</div>return ( <div> <p> Hello, {userId}! Your current active session is {sessionId}. </p> <buttononClick={fetchExternalData}>Fetch Data</button> </div> )}