title: How to solve CORS in firebase functions enviroment?
body: <p>I´m runnig into <code>CORS</code> issues. In my <code>functions/index.js</code> I have:</p>
  
  <pre><code>const cors = require('cors')({
    origin: true
  });
  </code></pre>
  
  <p>My endpoint is <code>https://sis-t.redsys.es:25443/sis/realizarPago</code>. I need to do a <code>POST</code> to this url with apropiate parameters from the client, and this is outside firebase enviroment. I also have the correct plan to allow external network, but making a request to a different address than the origin one, triggers a <code>CORS</code> problem:</p>
  
  <ul>
  <li><a href="https://i.stack.imgur.com/guUUX.jpg" rel="noreferrer">error log</a></li>
  <li><a href="https://i.stack.imgur.com/3WDfa.jpg" rel="noreferrer">network log</a></li>
  </ul>
  
  <p>I have read that you only need to modify the headers, but that only applies if you are making a request to your own server. When you do the <code>http.onRequest()</code>, you can use a middleware inside the function, but what happens when you make a POST to an external server?</p>
  
  <p>This is the <code>axios</code> functions that do the <code>POST</code>:</p>
  
  <pre><code>cardPay: function () {
    this.cardProcess = true
      axios({
        method: 'post',
        url: 'https://us-central1-cutre-windrider.cloudfunctions.net/cardPay',
        data: {
          verifiedUserLogged: this.userLogged.uid,
          cart: this.cartItemList,
          finalPrice: this.serverPrice,
          deliveryInfo: this.userLogged.deliveryAdress,
          name: this.userLogged.displayName || false,
          email: this.userLogged.email
        }
      })
      .then(response =&gt; {
        this.requestData = response
        this.redsysRedirect(response.data.data)
      })
      .catch(console.log)
      },
  redsysRedirect: function (data) {
    axios.post('https://sis-t.redsys.es:25443/sis/realizarPago', {
      'Ds_SignatureVersion': 'HMAC_SHA256_V1',
      'Ds_MerchantParameters': data.merchantParameters,
      'Ds_Signature': data.signature
    }).then(console.log).catch(console.log)
  </code></pre>
  
  <p>These are the server side functions:</p>
  
  <pre><code>exports.cardPay = functions.https.onRequest((req, res) =&gt; {
    return cors(req, res, () =&gt; {
      const cart = req.body.cart
      const user = req.body.verifiedUserLogged
      const key = admin.database().ref(`sales/${user}`).push().key
      processCart(cart).then(result =&gt; {
        console.info(createPayment(result, key))
        return res.json({ "data": createPayment(result, key) }).end()
      }).catch(console.log)
    })
  })
  
  function processCart(cart) {
    return new Promise((resolve, reject) =&gt; {
      Promise.all(cart.map(i =&gt; switcher(i)))
        .then(prices =&gt; resolve(
          prices.reduce(
            (finalPrice, price) =&gt; price + finalPrice, 0)
        )).catch(console.log)
    });
  }
  
  function switcher(item) {
    switch (item.destiny) {
      case 'bookedLessons':
          return lessonPrice(item.name, item.index)
        case 'bookedRentals':
          return rentalPrice(item.id, item.index, item.insurancePurchased, item.insuranceId)
        case 'bookedLodgins':
          return item.reservationData ? roomPriceWithReservation(item.id, item.quantity, item.persons, item.reservationData) : roomPriceNoReservation(item.id, item.quantity, item.persons)
        case 'deliveries':
          return productPrice(item.id, item.quantity)
        case 'bookedCar':
          return carPrice(item.id, item.index)
        case 'bookedStorage':
          return storagePrice(item.index)
        case 'bookedTransportation':
          return transportationPrice(item.id, item.index, item.persons, item.roundTrip)
        case 'bookedDoublePack':
          return doublePack(item.id, item.persons)
        case 'bookedTriplePack':
          return triplePack(item.id, item.persons)
        default:
          break
    }
  }
  
  function createPayment(total, orderId) {
    let redsys = new Redsys();
    let mParams = {
        "DS_MERCHANT_AMOUNT":total.toString(),
        "DS_MERCHANT_ORDER":orderId,
        "DS_MERCHANT_MERCHANTCODE":   "025988262",
        // "DS_MERCHANT_MERCHANTCODE":tpvInfo.fucCode,
        "DS_MERCHANT_CURRENCY":"978",
        // "DS_MERCHANT_CURRENCY":tpvInfo.currency,
        "DS_MERCHANT_TRANSACTIONTYPE":"0",
        // "DS_MERCHANT_TRANSACTIONTYPE":tpvInfo.transaction_type,
        "DS_MERCHANT_TERMINAL":   "001",
        // "DS_MERCHANT_TERMINAL":tpvInfo.terminal,
        "DS_MERCHANT_MERCHANTURL":'http://localhost:8080',
        "DS_MERCHANT_URLOK":'http://localhost:8080/home?foo=true',
        "DS_MERCHANT_URLKO":'http://localhost:8080/home?foo=false'
    };
    return  {signature: redsys.createMerchantSignature(/* tpvInfo.secret */   "sq7HjrUOBfKmC576ILgskD5srU870gJ7", mParams) , merchantParameters: redsys.createMerchantParameters(mParams), raw: mParams};
  }
  </code></pre>