useSession()
The useSession() hook provides access to the current user's Session object, as well as helpers for setting the active session.
Returns
There are multiple variants of this type available which you can select by clicking on one of the tabs.
- Name
isLoaded- Type
false- Description
A boolean that indicates whether Clerk has completed initialization. Initially
false, becomestrueonce Clerk loads.
- Name
isSignedIn- Type
undefined- Description
A boolean that indicates whether a user is currently signed in.
- Name
session- Type
undefined- Description
The current session for the user.
- Name
isLoaded- Type
true- Description
A boolean that indicates whether Clerk has completed initialization. Initially
false, becomestrueonce Clerk loads.
- Name
isSignedIn- Type
false- Description
A boolean that indicates whether a user is currently signed in.
- Name
session- Type
null- Description
The current session for the user.
- Name
isLoaded- Type
true- Description
A boolean that indicates whether Clerk has completed initialization. Initially
false, becomestrueonce Clerk loads.
- Name
isSignedIn- Type
boolean- Description
A boolean that indicates whether a user is currently signed in.
- Name
session- Type
- SignedInSessionResource
- Description
The current session for the user.
Example
Access the Session object
The following example uses the useSession() hook to access the Session object, which has the lastActiveAt property. The lastActiveAt property is a Date object used to show the time the session was last active.
import { useSession } from '@clerk/tanstack-react-start'
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/')({
component: HomePage,
})
function HomePage() {
const { isLoaded, session, isSignedIn } = useSession()
// Handle loading state
if (!isLoaded) return <div>Loading...</div>
// Protect the page from unauthenticated users
if (!isSignedIn) return <div>Sign in to view this page</div>
return (
<div>
<p>This session has been active since {session.lastActiveAt.toLocaleString()}</p>
</div>
)
}Feedback
Last updated on