Issue: `store_id` Header Not Included in Requests

I have a middleware function isValidStore that checks for the presence of a store_id header in incoming requests. However, req.headers does not include store_id and always returns undefined.

Middleware Code:

import Store from "../../database/models/stores/Store.model.js";

export const isValidStore = async (req, res, next) => {
  const storeId = req.headers.store_id;
  console.log(req.headers)
  console.log("storeId", storeId);
  if (!storeId) {
    return res.status(401).json({ error: "Store ID not provided" });
  }

  try {
    const store = await Store.findById(storeId).exec();

    if (!store) {
      return res.status(404).json({ error: "Store not found" });
    }

    if (store.isDisabled) {
      return res.status(403).json({ error: "Store is disabled" });
    }

    req.store = store;
    next();
  } catch (error) {
    console.error("Error validating store:", error);
    return res.status(500).json({ error: "Internal Server Error" });
  }
};

export const checkFeatureAccess = (packageTypes) => async (req, res, next) => {
  const store = req.store;

  if (!store) {
    return res.status(401).json({ error: "Store not valid" });
  }

  try {
    if (packageTypes.includes(store?.package)) {
      next();
    } else {
      return res.status(403).json({ error: "Store does not have access to this feature" });
    }
  } catch (error) {
    console.error("Error checking feature access:", error);
    return res.status(500).json({ error: "Internal Server Error" });
  }
};

Request Code:

const headers = {
  "Content-Type": "application/json",
  store_id: Constants.store_id,
};

export const registerWithEmailAndPassword = async ({
  first_name,
  last_name,
  email,
  password,
  confirm_password,
}) => {
  try {
    const { data } = await axios.post(
      `${Constants.api}/${Api_Routes.Register}`,
      { first_name, last_name, email, password, confirm_password },
      {
        headers,
      }
    );
  } catch (error) {
    console.log(error);
    toast.error(error.response?.data?.error || "An Error Occured");
  }
};

CORS Configuration:

const corsOptions = {
  origin: "*",
  allowedHeaders: ["Content-Type", "Authorization", "Store_id"],
  exposedHeaders: ["Store_id"],
  methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
};

import api_routes from "./routes/index.js";
const app = express();
app.use(bodyParser.json());
app.use(cors(corsOptions));
app.options("*", cors(corsOptions));

Observed req.headers Output:

{
  accept: 'application/json, text/plain, */*',
  host: 'localhost:8081',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
  'accept-encoding': 'gzip, deflate, br, zstd',
  'accept-language': 'en-US,en;q=0.9',
  origin: 'http://localhost:3000',
  referer: 'http://localhost:3000/',
  'x-request-id': 'e4f53805bf9fb188ea4ba074d00b5fbc',
  'x-real-ip': '',
  'x-forwarded-port': '443',
  'x-forwarded-scheme': 'https',
  'x-original-uri': '/api/auth/register',
  'x-scheme': 'https',
  'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '"Windows"',
  'sec-fetch-site': 'cross-site',
  'sec-fetch-mode': 'cors',
  'sec-fetch-dest': 'empty',
  priority: 'u=1, i',
  'x-original-proto': 'https',
  'x-forwarded-proto': 'https',
  'x-forwarded-host': '',
  'x-forwarded-for': '',
  'proxy-connection': 'Keep-Alive',
  'content-type': 'application/json',
  'content-length': '133'
}

Questions:

  1. Why is the store_id header not appearing in req.headers?
  2. Is there any additional configuration needed to ensure the store_id header is included in the request?

Any help or insights would be greatly appreciated!

Steps Taken:

  1. Verified that store_id is included in the headers object in the request code.
  2. Checked CORS configuration to ensure Store_id is listed in allowedHeaders.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật