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> ) }
VariablesDescription
isLoadedA boolean that until Clerk loads and initializes, will be set to false. Once Clerk loads, isLoaded will be set to true
setActiveA function that sets the active session, is most cases this should be used over setSession()
setSessionA function that sets the active session, this should
sessionHolds the current active session for the user

Was this helpful?

Clerk © 2023