Sign in

Read Authentication State

This guide shows how to read authentication and authorization state in a React application using the useAuth() hook from the MonoCloud SDK.

These helpers are useful when you want to conditionally render UI, check permissions, or call APIs.

When to use the hook

Use useAuth() when you need information about the current session inside a component.

Typical use cases include:

  • Showing different UI for signed-in vs signed-out users
  • Reading the current user profile
  • Conditionally enabling features

Before you begin

This guide assumes you’ve completed the React quickstart or the installation guide.

You should already have:

  • A Single Page App configured in MonoCloud
  • The @monocloud/auth-react SDK installed
  • Your app wrapped with <MonoCloudAuthProvider>
useAuth() can only be called from a component rendered inside <MonoCloudAuthProvider>. Calling it outside the provider throws a MonoCloudJsError.

The useAuth() hook

useAuth() returns the current authentication state along with the available actions.

src/Profile.tsx
import { useAuth } from "@monocloud/auth-react";

export default function Profile() {
  const { isLoading, isAuthenticated, user } = useAuth();

  if (isLoading) {
    return <p>Loading...</p>;
  }

  if (!isAuthenticated) {
    return <p>Please sign in</p>;
  }

  return <p>Hi {user?.email}</p>;
}

State

ValueDescription
isLoadingtrue while the authentication state is still being resolved
isAuthenticatedtrue when a session exists
userThe authenticated user's profile
sessionThe full session, including the user and tokens
errorThe error encountered during authentication, if any

Actions

ActionDescription
signInStarts the sign-in flow
signOutStarts the sign-out flow
signInSilentRestores the session silently via a hidden iframe (prompt=none)
refreshSessionRenews the tokens using the Refresh Token Grant
refetchUserInfoRefetches the profile from the UserInfo endpoint
getTokensReturns the active tokens, refreshing them if expired

Read the current user

The user object contains the profile claims returned by MonoCloud.

src/Profile.tsx
import { useAuth } from "@monocloud/auth-react";

export default function Profile() {
  const { isAuthenticated, user } = useAuth();

  if (!isAuthenticated) {
    return null;
  }

  return (
    <div>
      <p>{user?.email}</p>
      <pre>{JSON.stringify(user, null, 2)}</pre>
    </div>
  );
}

Read the full session

The session includes the user and the tokens issued for the current session.

tsx
const { session } = useAuth();

console.log(session?.user.sub);

Access the underlying client

For advanced, lower-level operations that useAuth() does not cover, use the useClient() hook. It returns the underlying MonoCloudWebJSClient created by the provider - for example, to revoke a token via client.oidcClient.

Most applications only need useAuth(). Reach for useClient() only when you need an operation it does not expose.
src/RevokeButton.tsx
import { useAuth, useClient } from "@monocloud/auth-react";

export default function RevokeButton() {
  const { getTokens } = useAuth();
  const client = useClient();

  const revoke = async () => {
    const tokens = await getTokens();
    await client.oidcClient.revokeToken(tokens.accessToken);
  };

  return <button onClick={() => revoke()}>Revoke token</button>;
}

Like useAuth(), useClient() must be called from a component rendered inside <MonoCloudAuthProvider>, otherwise it throws a MonoCloudJsError.

© 2024 MonoCloud. All rights reserved.