Uecko_ERP/apps/server/src/contexts/auth/presentation/middleware/tab-context.middleware.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-02-04 14:58:33 +00:00
import { ApiError, ExpressController } from "@common/presentation";
import { NextFunction, Request, Response } from "express";
import httpStatus from "http-status";
export const validateTabHeader = (req: Request, res: Response, next: NextFunction) => {
const tabId = req.headers["x-tab-id"];
if (!tabId) {
return ExpressController.errorResponse(
new ApiError({
status: 401,
title: httpStatus["401"],
name: httpStatus["401_NAME"],
detail: "Tab ID is required",
}),
res
);
}
next();
};
export const validateTabContext = async (req: Request, res: Response, next: NextFunction) => {
const tabId = req.headers["x-tab-id"];
if (!tabId) {
return ExpressController.errorResponse(
new ApiError({
status: 401,
title: httpStatus["401"],
name: httpStatus["401_NAME"],
detail: "Tab ID is required",
}),
res
);
}
2025-02-04 18:25:10 +00:00
const contextOrError = await TabContextRepository.getContextByTabId(tabId);
2025-02-04 14:58:33 +00:00
if (contextOrError.isFailure) {
return ExpressController.errorResponse(
new ApiError({
status: 401,
title: httpStatus["401"],
name: httpStatus["401_NAME"],
detail: "Invalid or expired Tab ID",
}),
res
);
}
const context = contextOrError.data;
req.user = { id: context.user_id, company_id: context.company_id };
next();
};