Skip to main content

apps/gateway/src/middleware/tenant-guard.ts

Metadata

Indexed Symbols

No indexed functions/methods detected in this file.

Markdown Headings (if applicable)

No markdown headings detected.

Source Preview

import type { FastifyPluginAsync } from "fastify";
import { isNonTenantRoute, isPublicRoute } from "../utils/routes.js";
import { getWorkspaceIdFromHeaders } from "../utils/workspace.js";

export const tenantGuardPlugin: FastifyPluginAsync = async (app) => {
app.addHook("preHandler", async (request, reply) => {
if (isPublicRoute(request.url)) {
return;
}

const authContext = request.auth;
if (!authContext) {
reply.code(401);
throw new Error("Authentication is required");
}

if (isNonTenantRoute(request.url)) {
return;
}

let workspaceId: string;
try {
workspaceId = getWorkspaceIdFromHeaders(request.headers as Record<string, unknown>);
} catch (error) {
reply.code(400);