Overview
useSession hook is used to access the current session and user details from the SessionContext.
Example Usage
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
current session object.type Session = {
sessionId: string;
sessionToken: string;
userId: string;
};
function to log out the current user.
details of the currently signed-in user.type UserDetails = {
firstName: string;
lastName: string;
phoneNumber: string;
email: string;
isEmailVerified: boolean;
isPhoneVerified: boolean;
userState: string;
displayName: string;
metadata?: Record<string, string>;
};
boolean indicating whether the user is signed in.
boolean indicating whether the user details has been fetched.
boolean indicating whether the user details is loading.
boolean indicating whether an error occurred while fetching user details.
error
HttpError | Error | undefined
error object if an error occurred while fetching user details.