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.
Use useAuth() when you need information about the current session inside a component.
Typical use cases include:
This guide assumes you’ve completed the React quickstart or the installation guide.
You should already have:
@monocloud/auth-react SDK installed<MonoCloudAuthProvider>useAuth()can only be called from a component rendered inside<MonoCloudAuthProvider>. Calling it outside the provider throws aMonoCloudJsError.
useAuth() hookuseAuth() returns the current authentication state along with the available actions.
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>;
}
| Value | Description |
|---|---|
isLoading | true while the authentication state is still being resolved |
isAuthenticated | true when a session exists |
user | The authenticated user's profile |
session | The full session, including the user and tokens |
error | The error encountered during authentication, if any |
| Action | Description |
|---|---|
signIn | Starts the sign-in flow |
signOut | Starts the sign-out flow |
signInSilent | Restores the session silently via a hidden iframe (prompt=none) |
refreshSession | Renews the tokens using the Refresh Token Grant |
refetchUserInfo | Refetches the profile from the UserInfo endpoint |
getTokens | Returns the active tokens, refreshing them if expired |
The user object contains the profile claims returned by MonoCloud.
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>
);
}
The session includes the user and the tokens issued for the current session.
const { session } = useAuth();
console.log(session?.user.sub);
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 needuseAuth(). Reach foruseClient()only when you need an operation it does not expose.
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.