useSession()
The useSession()
hook provides access to the current user's Session
object, as well as helpers for setting the active session.
useSession()
returns
Name | Type | Description |
---|---|---|
isLoaded | boolean | A boolean that is set to false until Clerk loads and initializes. |
isSignedIn | boolean | A boolean that is set to true if the user is signed in. |
session | Session | Holds the current active session for the user. |
How to use the useSession()
hook
The following example demonstrates how to use the useSession()
hook to access the SignIn
object, which has the lastActiveAt
property on it. The lastActiveAt
property is used to display the last active time of the current session to the user.
home.tsximport { useSession } from "@clerk/clerk-react"; export default function Home() { const { isLoaded, session, isSignedIn } = useSession(); if (!isLoaded) { // Add logic to handle loading state return null; } if(!isSignedIn) { // Add logic to handle not signed in state return null; } return ( <div> <p>This session has been active since {session.lastActiveAt.toLocaleString()} </p> </div> ) }
Last updated on February 22, 2024