Preview
Changelog

Read who is signed in from your React components

Released in v0.8.0

useSession() lets your header, account menu or route guard ask whether a user is signed in, and who. Every component on the page that uses it shares one read of the session from the server.

import { useSession } from "@zitadel/sdk-react";

export function AccountMenu() {
  const { status, session } = useSession({ project });
  if (status === "loading") return null;
  return status === "authenticated"
    ? <a href="/profile">{session?.display ?? "Account"}</a>
    : <a href="/login">Sign in</a>;
}
  • status is loading, then authenticated or unauthenticated. error means there is no answer yet, and its code says whether no project is configured or the read failed.
  • session holds the user's id, identifier and display name, never a token.
  • refresh() reads again, for example after a sign-in completes in place. A refresh that fails keeps the last answer.
  • Outside components, sessionStore() gives a route guard or loader the same shared read, and getSession() makes a single one.
  • zitadel setup points the agents of a React app at the hook.

React SDK

Pull request#3264