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> ) }
Values | Description |
---|---|
isLoaded | A 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() . |
session | Holds the current active session for the user. |