Docs

useSession()

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

Returns

  • Name
    isLoaded
    Type
    boolean
    Description
  • Name
    isSignedIn
    Type
    boolean
    Description

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

  • Name
    session
    Type
    Session
    Description

    Holds the current active session for the user.

How to use the useSession() hook

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.

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

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

  if (!isLoaded) {
    // Handle loading state
    return null
  }
  if (!isSignedIn) {
    // Handle signed out 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