2025-08-26 18:55:59 +00:00
|
|
|
import { ExpressController, UnauthorizedApiError } from "@erp/core/api";
|
2025-08-14 14:58:13 +00:00
|
|
|
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);
|
2025-08-14 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
};
|
|
|
|
|
}
|