naystack - v1.5.10
    Preparing search index...

    Function getContext

    • Builds the auth context from a NextRequest: reads either the Authorization: Bearer <token> header or the refresh cookie. Use this in REST API routes (outside GraphQL) to identify the current user.

      The GraphQL server uses this automatically — you typically only need it for custom REST endpoints.

      Parameters

      • req: NextRequest

        The NextRequest (headers and cookies are read).

      Returns Context

      Context with userId: number | null. If the user was identified via the refresh cookie (not an access token), isRefreshID is set to true.

      import { getContext } from "naystack/auth";

      export const POST = async (req: NextRequest) => {
      const ctx = getContext(req);
      if (!ctx?.userId) return new NextResponse("Unauthorized", { status: 401 });
      // ctx.userId is the authenticated user's id
      };