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.tsximport { 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.tsximport { 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> ) }
Variables | Description |
---|---|
isLoaded | A boolean that until Clerk loads and initializes, will be set to false. Once Clerk loads, isLoaded will be set to true |
setActive | A function that sets the active session, is most cases this should be used over setSession() |
setSession | A function that sets the active session, this should |
session | Holds the current active session for the user |