Docs

useSession()

The useSession() hook provides access to the current user's Session object, as well as helpers for setting the active session.

useSession() returns

  • Name
    isLoaded
    Type
    boolean
    Description

    A boolean that is set to false until Clerk loads and initializes.

  • Name
    isSignedIn
    Type
    boolean
    Description

    A boolean that is set to true if the user is signed in.

  • Name
    session
    Type
    Session
    Description

    Holds the current active session for the user.

How to use the useSession() hook

The following example demonstrates how to use the useSession() hook to access the SignIn object, which has the lastActiveAt property on it. The lastActiveAt property is used to display the last active time of the current session to the user.

home.tsx
import { useSession } from '@clerk/clerk-react'

export default function Home() {
  const { isLoaded, session, isSignedIn } = useSession()

  if (!isLoaded) {
    // Add logic to handle loading state
    return null
  }
  if (!isSignedIn) {
    // Add logic to handle not signed in state
    return null
  }

  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