Files
musadeq/backend/src/common/middleware/tenant.middleware.ts

33 lines
1.3 KiB
TypeScript

/**
* ════════════════════════════════════════════════════════════
* مُصادَق (Musadaq) — Tenant Middleware
* ════════════════════════════════════════════════════════════
* يستخرج tenantId من JWT ويضعه في request.
* يستخدم في تصفية البيانات (Query filtering).
* ════════════════════════════════════════════════════════════
*/
import {
Injectable,
NestMiddleware,
UnauthorizedException,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class TenantMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
const { user } = req as any;
if (!user || !user.tenantId) {
// Phase 1: Not always required (registration, login)
return next();
}
// Set tenant context for the request
(req as any).tenantId = user.tenantId;
next();
}
}