setTenant = (req, res, next) => {
  // for a saltcorn mobile request use 'req.user.tenant'
  if (req.smr) {
    if (req.user?.tenant && req.user.tenant !== db.connectObj.default_schema) {
      const state = getTenant(req.user.tenant);
      if (!state) {
        setLanguage(req, res);
        next();
      } else {
        db.runWithTenant({ tenant: req.user.tenant, req }, () => {
          setLanguage(req, res, state);
          state.log(5, `${req.method} ${req.originalUrl}`);
          next();
        });
      }
    } else {
      setLanguage(req, res);
      next();
    }
  } else {
    const other_domain = get_other_domain_tenant(req.hostname);
    if (other_domain) {
      const state = getTenant(other_domain);
      if (!state) {
        setLanguage(req, res);
        next();
      } else {
        db.runWithTenant({ tenant: other_domain, req }, () => {
          setLanguage(req, res, state);
          if (state.logLevel >= 5)
            state.log(
              5,
              `${req.method} ${req.originalUrl}${
                state.getConfig("log_ip_address", false) ? ` IP=${req.ip}` : ""
              }`
            );
          next();
        });
      }
    } else {
      const ten = get_tenant_from_req(req);
      const state = getTenant(ten);
      if (!state) {
        setLanguage(req, res);
        next();
      } else {
        db.runWithTenant({ tenant: ten, req }, () => {
          setLanguage(req, res, state);
          if (state.logLevel >= 5)
            state.log(
              5,
              `${req.method} ${req.originalUrl}${
                state.getConfig("log_ip_address", false) ? ` IP=${req.ip}` : ""
              }`
            );
          next();
        });
      }
    }
  }
}
middleware to extract the tenant domain and call runWithtenant()
@param {object} req
@param {object} res
@param {function} next