Uecko_ERP/modules/auth/src/api/lib/express/tenancy.middleware.ts

18 lines
603 B
TypeScript
Raw Normal View History

2025-08-26 18:55:59 +00:00
import { ExpressController, UnauthorizedApiError } from "@erp/core/api";
import { NextFunction, Response } from "express";
import { RequestWithAuth } from "./auth-types";
/**
* Middleware que exige presencia de usuario y companyId.
* Debe ir DESPUÉS del middleware de autenticación.
*/
export function enforceTenant() {
return (req: RequestWithAuth, res: Response, next: NextFunction) => {
// Validación básica del tenant
if (!req.user || !req.user.companyId) {
2025-08-26 18:55:59 +00:00
return ExpressController.errorResponse(new UnauthorizedApiError("Unauthorized"), req, res);
}
next();
};
}