By default, Next.js opts all routes into static rendering. If you need to opt a route or routes into dynamic rendering because you need to access the authentication data at request time, you can create a boundary by passing the dynamic prop to <ClerkProvider>. See the guide on rendering modes for more information, including code examples.
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.
This function returns a discriminated union type. There are multiple variants of this type available which you can select by clicking on one of the tabs.
Initialization
Signed out
Signed in (no active organization)
Signed in (with active organization)
Name
actor
Type
undefined
Description
The JWT actor for the session. Holds identifier for the user that is impersonating the current user. Read more about impersonation.
Name
getToken()
Type
(options?) => Promise<null | string>
Description
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 signs out the current user. Returns a promise that resolves when complete. See the reference docJavaScript Icon.
Name
userId
Type
undefined
Description
The ID of the current user.
Name
actor
Type
null
Description
The JWT actor for the session. Holds identifier for the user that is impersonating the current user. Read more about impersonation.
Name
getToken()
Type
(options?) => Promise<null | string>
Description
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
(params) => false
Description
A function that checks if the user has specific permissions or roles. See the reference docClerk Icon.
Name
isLoaded
Type
true
Description
A boolean that indicates whether Clerk has completed initialization. Initially false, becomes true once Clerk loads.
Name
isSignedIn
Type
false
Description
A boolean that indicates whether a user is currently signed in.
Name
orgId
Type
null
Description
The ID of the user's active organization.
Name
orgRole
Type
null
Description
The current user's role in their active organization.
Name
orgSlug
Type
null
Description
The URL-friendly identifier of the user's active organization.
A function that signs out the current user. Returns a promise that resolves when complete. See the reference docJavaScript Icon.
Name
userId
Type
null
Description
The ID of the current user.
Name
actor
Type
null | { [x: string]: unknown; sub: string; }
Description
The JWT actor for the session. Holds identifier for the user that is impersonating the current user. Read more about impersonation.
Name
getToken()
Type
(options?) => Promise<null | string>
Description
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
(isAuthorizedParams) => boolean
Description
A function that checks if the user has specific permissions or roles. See the reference docClerk Icon.
Name
isLoaded
Type
true
Description
A boolean that indicates whether Clerk has completed initialization. Initially false, becomes true once Clerk loads.
Name
isSignedIn
Type
true
Description
A boolean that indicates whether a user is currently signed in.
Name
orgId
Type
null
Description
The ID of the user's active organization.
Name
orgRole
Type
null
Description
The current user's role in their active organization.
Name
orgSlug
Type
null
Description
The URL-friendly identifier of the user's active organization.
A function that signs out the current user. Returns a promise that resolves when complete. See the reference docJavaScript Icon.
Name
userId
Type
string
Description
The ID of the current user.
Name
actor
Type
null | { [x: string]: unknown; sub: string; }
Description
The JWT actor for the session. Holds identifier for the user that is impersonating the current user. Read more about impersonation.
Name
getToken()
Type
(options?) => Promise<null | string>
Description
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
(isAuthorizedParams) => boolean
Description
A function that checks if the user has specific permissions or roles. See the reference docClerk Icon.
Name
isLoaded
Type
true
Description
A boolean that indicates whether Clerk has completed initialization. Initially false, becomes true once Clerk loads.
Name
isSignedIn
Type
true
Description
A boolean that indicates whether a user is currently signed in.
Name
orgId
Type
string
Description
The ID of the user's active organization.
Name
orgRole
Type
OrganizationCustomRoleKey
Description
The current user's role in their active organization.
Name
orgSlug
Type
null | string
Description
The URL-friendly identifier of the user's active organization.
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.
React
Next.js
src/pages/ExternalDataPage.tsx
import { useAuth } from"@clerk/clerk-react";exportdefaultfunctionExternalDataPage() {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(); };if (!isLoaded) {return <div>Loading...</div>; }if (!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> );}
app/external-data/page.tsx
"use client";import { useAuth } from"@clerk/nextjs";exportdefaultfunctionExternalDataPage() {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(); };if (!isLoaded) {return <div>Loading...</div>; }if (!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> );}