"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateUser = void 0;
const callControlPlaneApi_1 = require("../../util/callContolPlane/callControlPlaneApi");
// Middleware to validate user token and attach user info to request
const validateUser = async (req, res, next) => {
    const token = req.headers['x-access-token'] || req.headers.authorization;
    try {
        const response = await (0, callControlPlaneApi_1.callControlPlane)(`/validate`, {
            method: 'GET',
            params: {
                ...req.body,
                ...req.query,
                ...req.params,
            },
        }, token);
        if (response?.status === 1) {
            req.auth = { payload: response?.response?.signedInUser };
            next();
        }
        else {
            res.status(401).send({ message: 'Unauthorized Access' });
        }
    }
    catch (error) {
        console.error('Error while authorizing user:', error);
        res.status(403).send({
            status: 0,
            message: error.message,
        });
    }
};
exports.validateUser = validateUser;
//# sourceMappingURL=validateUser.js.map