Skip to Content
Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev

useSession()

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

Usage

pages/index.tsx
import { useSession } from "@clerk/nextjs"; export default function Home() { const { isLoaded, session, isSignedIn } = useSession(); if (!isLoaded) { // handle loading state return null; } if(!isSignedIn) { // handle not signed in state return null; } return ( <div> <p>This session has been active since {session.lastActiveAt.toLocaleString()} </p> </div> ) }
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 not signed in state return null; } return ( <div> <p>This session has been active since {session.lastActiveAt.toLocaleString()} </p> </div> ) }
ValuesDescription
isLoadedA boolean that is set to false until Clerk loads and initializes.
setActive()A function that sets the active session.
setSession() (deprecated)Deprecated in favor of setActive().
sessionHolds the current active session for the user.

What did you think of this content?

Clerk © 2024