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.
| Parameter | Type | Description |
|---|---|---|
options? | GetSessionOptions | Optional configuration to control session retrieval behavior. |
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>;
}
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).
| Parameter | Type | Description |
|---|---|---|
req | Request | NextRequest | Incoming request used to read authentication cookies and headers to resolve the current user's session. |
options? | GetSessionOptions | Optional configuration to control session retrieval behavior. |
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();
}
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.
| Parameter | Type | Description |
|---|---|---|
req | Request | NextRequest | Incoming request used to read authentication cookies and headers to resolve the current user's session. |
res | Response | NextResponse<unknown> | Response object to update when session resolution requires refreshed authentication cookies or headers. |
options? | GetSessionOptions | Optional configuration to control session retrieval behavior. |
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 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;
}
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.
| Parameter | Type | Description |
|---|---|---|
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. |
options? | GetSessionOptions | Optional configuration to control session retrieval behavior. |
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
}
};
};