useSession()
The useSession() hook provides access to the current user's Session object, as well as helpers for setting the active session.
Parameters
- Name
 options?- Type
 { treatPendingAsSignedOut?: boolean; }- Description
 An object containing options for the
useSession()hook.
- Name
 options.treatPendingAsSignedOut?- Type
 boolean- Description
 A boolean that indicates whether pending sessions are considered as signed out or not. Defaults to
true.
Returns
This function returns a discriminated union type. There are multiple variants of this type available which you can select by clicking on one of the tabs.
- Name
 isLoaded- Type
 false- Description
 A boolean that indicates whether Clerk has completed initialization. Initially
false, becomestrueonce Clerk loads.
- Name
 isSignedIn- Type
 undefined- Description
 A boolean that indicates whether a user is currently signed in.
- Name
 session- Type
 undefined- Description
 The current active session for the user.
- Name
 isLoaded- Type
 true- Description
 A boolean that indicates whether Clerk has completed initialization. Initially
false, becomestrueonce Clerk loads.
- Name
 isSignedIn- Type
 false- Description
 A boolean that indicates whether a user is currently signed in.
- Name
 session- Type
 null- Description
 The current active session for the user.
- Name
 isLoaded- Type
 true- Description
 A boolean that indicates whether Clerk has completed initialization. Initially
false, becomestrueonce Clerk loads.
- Name
 isSignedIn- Type
 true- Description
 A boolean that indicates whether a user is currently signed in.
- Name
 session- Type
 SignedInSessionResource- Description
 The current active session for the user.
Example
Access the Session object
The following example uses the useSession() hook to access the Session object, which has the lastActiveAt property. The lastActiveAt property is a Date object used to show the time the session was last active.
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 signed out state
    return null;
  }
  return (
    <div>
      <p>
        This session has been active since{" "}
        {session.lastActiveAt.toLocaleString()}
      </p>
    </div>
  );
}"use client";
import { useSession } from "@clerk/nextjs";
export default function HomePage() {
  const { isLoaded, session, isSignedIn } = useSession();
  if (!isLoaded) {
    // Handle loading state
    return null;
  }
  if (!isSignedIn) {
    // Handle signed out state
    return null;
  }
  return (
    <div>
      <p>
        This session has been active since{" "}
        {session.lastActiveAt.toLocaleString()}
      </p>
    </div>
  );
}Feedback
Last updated on