Sign in

Function: getSession

Call Signature

getSession(options?: GetSessionOptions): Promise<MonoCloudSession | undefined>

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.

Parameters

ParameterTypeDescription
options?GetSessionOptionsOptional configuration to control session retrieval behavior.

Returns

Promise<MonoCloudSession | undefined>

Returns the resolved session, or undefined if none exists.

Examples

src/app/page.tsx
import { getSession } from "@monocloud/auth-nextjs";

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

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

Call Signature

getSession(req: Request | NextRequest, options?: GetSessionOptions): Promise<MonoCloudSession | undefined>

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

ParameterTypeDescription
reqRequest | NextRequestIncoming request used to read authentication cookies and headers to resolve the current user's session.
options?GetSessionOptionsOptional configuration to control session retrieval behavior.

Returns

Promise<MonoCloudSession | undefined>

Returns the resolved session, or undefined if none exists.

Examples

src/proxy.ts
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();
}

Call Signature

getSession(req: Request | NextRequest, res: Response | NextResponse<unknown>, options?: GetSessionOptions): Promise<MonoCloudSession | undefined>

Retrieves the current user's session using explicit request and response objects.

Use this overload when you have already created a response and want refreshed authentication cookies or headers applied to it.

Parameters

ParameterTypeDescription
reqRequest | NextRequestIncoming request used to read authentication cookies and headers to resolve the current user's session.
resResponse | NextResponse<unknown>Response object to update when session resolution requires refreshed authentication cookies or headers.
options?GetSessionOptionsOptional configuration to control session retrieval behavior.

Returns

Promise<MonoCloudSession | undefined>

Returns the resolved session, or undefined if none exists.

Examples

src/proxy.ts
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;
}

Call Signature

getSession(req: NextApiRequest | IncomingMessage, res: NextApiResponse | ServerResponse<IncomingMessage>, options?: GetSessionOptions): Promise<MonoCloudSession | undefined>

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

ParameterTypeDescription
reqNextApiRequest | IncomingMessageIncoming Node.js request used to read authentication cookies and resolve the current user's session.
resNextApiResponse | ServerResponse<IncomingMessage>Outgoing Node.js response used to apply refreshed authentication cookies when required.
options?GetSessionOptionsOptional configuration to control session retrieval behavior.

Returns

Promise<MonoCloudSession | undefined>

Returns the resolved session, or undefined if none exists.

Examples

src/pages/index.tsx
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
    }
  };
};
© 2024 MonoCloud. All rights reserved.