apps/gateway/src/middleware/tenant-guard.ts
Metadata
- Purpose: Gateway middleware for cross-cutting concerns.
- Domain:
applications - Language:
ts - Bytes: 1057
- Lines: 36
- Content hash (short):
c00d317e - Source (start): apps/gateway/src/middleware/tenant-guard.ts:1
- Source (end): apps/gateway/src/middleware/tenant-guard.ts:36
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);