Skip to main content

Overview

useSession hook is used to access the current session and user details from the SessionContext.

Example Usage

session-component.tsx
import { useSession } from '@locai1/iam-react';

const SIGN_IN_PAGE = "/sign-in";

const Header = () => {
  const { isSignedIn, logout } = useSession();

  return (
    <header>
      {isSignedIn && (
        <button onClick={logout}>
          Sign Out
        </button>
      )}
      {!isSignedIn && (
        <Link href={SIGN_IN_PAGE}>
          Sign In
        </Link>
      )}
    </header>
  );
};

Returns

session
Session | undefined
current session object.
Session type
type Session = {
  sessionId: string;
  sessionToken: string;
  userId: string;
};
logout
() => void
function to log out the current user.
userDetails
UserDetails | undefined
details of the currently signed-in user.
UserDetails type
type UserDetails = {
  firstName: string;
  lastName: string;
  phoneNumber: string;
  email: string;
  isEmailVerified: boolean;
  isPhoneVerified: boolean;
  userState: string;
  displayName: string;
  metadata?: Record<string, string>;
};
isSignedIn
boolean
boolean indicating whether the user is signed in.
isFetched
boolean
boolean indicating whether the user details has been fetched.
isLoading
boolean
boolean indicating whether the user details is loading.
isError
boolean
boolean indicating whether an error occurred while fetching user details.
error
HttpError | Error | undefined
error object if an error occurred while fetching user details.