Skip to main content
Docs

useSession()

The useSession() hook provides access to the current user's SessionJavaScript Icon 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, becomes true once 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, becomes true once 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, becomes true once Clerk loads.

  • Name
    isSignedIn
    Type
    boolean
    Description

    A boolean that indicates whether a user is currently signed in.

  • Name
    session
    Type
    SignedInSessionResourceJavaScript Icon
    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.

app/routes/index.tsx
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

What did you think of this content?

Last updated on