MonoCloud Authentication SDK
    Preparing search index...
    • Retrieves the current user's session using the active server request context.

      Intended for Server Components, Server Actions, Route Handlers, and Middleware where the request is implicitly available.

      Returns Promise<MonoCloudSession | undefined>

      Returns the resolved session, or undefined if none exists.

      import { getSession } from "@monocloud/auth-nextjs";

      export default async function Home() {
      const session = await getSession();

      return <div>{session?.user.name}</div>;
      }
      "use server";

      import { getSession } from "@monocloud/auth-nextjs";

      export async function getUserAction() {
      const session = await getSession();

      return { name: session?.user.name };
      }
      import { getSession } from "@monocloud/auth-nextjs";
      import { NextResponse } from "next/server";

      export const GET = async () => {
      const session = await getSession();

      return NextResponse.json({ name: session?.user.name });
      };
      import { getSession } from "@monocloud/auth-nextjs";
      import { NextResponse } from "next/server";

      export default async function proxy() {
      const session = await getSession();

      if (!session) {
      return new NextResponse("User not signed in", { status: 401 });
      }

      return NextResponse.next();
      }
    • Retrieves the current user's session using an explicit Web or Next.js request.

      Use this overload when you already have access to a Request or NextRequest (for example in Middleware or Route Handlers).

      Parameters

      • req: Request | NextRequest

        Incoming request used to read authentication cookies and headers to resolve the current user's session.

      • Optionalres: Response | NextResponse<unknown>

        Optional response to update if session resolution requires refreshed authentication cookies or headers.

      Returns Promise<MonoCloudSession | undefined>

      Returns the resolved session, or undefined if none exists.

      import { getSession } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export default async function proxy(req: NextRequest) {
      const session = await getSession(req);

      if (!session) {
      return new NextResponse("User not signed in", { status: 401 });
      }

      return NextResponse.next();
      }
      import { getSession } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export default async function proxy(req: NextRequest) {
      const res = NextResponse.next();

      const session = await getSession(req, res);

      if (!session) {
      return new NextResponse("User not signed in", { status: 401 });
      }

      res.headers.set("x-auth-status", "active");

      return res;
      }
      import { getSession } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export const GET = async (req: NextRequest) => {
      const session = await getSession(req);

      return NextResponse.json({ name: session?.user.name });
      };
      import { getSession } from "@monocloud/auth-nextjs";
      import { NextRequest, NextResponse } from "next/server";

      export const GET = async (req: NextRequest) => {
      const res = new NextResponse("YOUR CUSTOM RESPONSE");

      const session = await getSession(req, res);

      if (session?.user) {
      res.cookies.set("something", "important");
      }

      return res;
      };
    • Retrieves the current user's session in the Pages Router or Node.js runtime.

      Use this overload in API routes or getServerSideProps, where Node.js request and response objects are available.

      Parameters

      • req: NextApiRequest | IncomingMessage

        Incoming Node.js request used to read authentication cookies and resolve the current user's session.

      • res: NextApiResponse | ServerResponse<IncomingMessage>

        Outgoing Node.js response used to apply refreshed authentication cookies when required.

      Returns Promise<MonoCloudSession | undefined>

      Returns the resolved session, or undefined if none exists.

      import { getSession, MonoCloudSession } from "@monocloud/auth-nextjs";
      import { GetServerSideProps } from "next";

      type Props = {
      session?: MonoCloudSession;
      };

      export default function Home({ session }: Props) {
      return <pre>Session: {JSON.stringify(session, null, 2)}</pre>;
      }

      export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
      const session = await getSession(ctx.req, ctx.res);

      return {
      props: {
      session
      }
      };
      };
      import { getSession } from "@monocloud/auth-nextjs";
      import { NextApiRequest, NextApiResponse } from "next";

      export default async function handler(
      req: NextApiRequest,
      res: NextApiResponse
      ) {
      const session = await getSession(req, res);

      res.status(200).json({ name: session?.user.name });
      }