This guide shows how to read authentication and authorization state in a Next.js application using the MonoCloud SDK.
These helpers are useful when you want to conditionally render UI, check permissions, or call APIs.
Use these methods when you need information about the current session.
Typical use cases include:
This guide assumes you’ve completed the relevant Next.js quickstart:
You should already have:
.env.localUse isAuthenticated() to check whether the current request has a valid user session.
const isSignedIn = await isAuthenticated();
Behavior
true if the user is signed infalse if no valid session existsExample: Conditional UI (Server Component)
import { isAuthenticated } from "@monocloud/auth-nextjs";
export default async function Header() {
const isSignedIn = await isAuthenticated();
return (
<header>
{isSignedIn ? "Welcome back" : "Please sign in"}
</header>
);
}
Use this when you only need to check whether the user is authenticated.
Use getSession() to retrieve the full session, including the authenticated user.
const session = await getSession();
Behavior
undefined if the user is not authenticatedExample
const session = await getSession();
if (!session) {
return null;
}
console.log(session.user.email);
When to use getSession()
Use getTokens() to access the current user’s tokens.
const tokens = await getTokens();
Behavior
MonoCloudValidationError if the session does not exist or tokens cannot be found/refreshed.Common use case: calling your API
const tokens = await getTokens();
await fetch("https://api.example.com/data", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`,
},
});
Notes
server-onlyUse isUserInGroup() to check whether the current user belongs to a specific group.
const isAdmin = await isUserInGroup(["admin"]);
Behavior
false if the user is unauthenticatedtrue if the user belongs to the groupfalse otherwiseExample
if (await isUserInGroup(["admin"])) {
// enable admin feature
}
To use group-based checks:
groups to the application scopes.env.local:MONOCLOUD_AUTH_SCOPES=openid profile email groups
After updating scopes, users must sign out and sign back in for the new claims to be included in their session.
Continue securing your application: