1

I've followed several answers on this topic, but none of them are working for me.

I'm trying to enable CORS within my Cloud Function. This function returns a video resource to be played in a WebGL application made with Unity.

This is the index.js code:

const functions = require('firebase-functions');
const cors = require('cors')({origin: true}); 

var admin = require("firebase-admin");

var serviceAccount = require("./something-eb929f7e59.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://app.firebaseio.com"
});

var bucket = admin.storage().bucket("gs://app.appspot.com");

exports.getFileTour = functions.https.onRequest((request, response) => {

    cors((request, response) => {

      response.set('Access-Control-Allow-Origin', "*");
      response.set('Access-Control-Allow-Methods', 'GET');
      response.set('Access-Control-Allow-Headers', 'Content-Type');
      var u = request.query.u;
      var p = request.query.p;
      var filepath = request.query.fp;
      var url_out = '';
      const file = bucket.file(filepath);
      file.getSignedUrl({
          action: 'read',
          expires: '03-17-5000'
      }, function (err, url) {
          if (err) {
              console.error(err);
              return;
          }
          url_out = url;
          response.send(url_out);

          //console.log(request.que);

          });
   });
});

And this is my package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.12.1",
    "firebase-functions": "^1.0.3",
    "cors": "^2.8.5"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true
}

I've deployed the application with Firebase CLI after installing cors in the local folder (I don't know if this is necessary, though).

But I keep receiving this error in the browser's console:

Access to XMLHttpRequest at 'https://resource.mp4' from origin 'https://origin.firebaseapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Could someone give some advice on this?

Cheers!

MorbidDesign
  • 40
  • 12
  • You can refer to a similar question asked previously [here](https://stackoverflow.com/questions/42755131/enabling-cors-in-cloud-functions-for-firebase) – Divyesh Puri Dec 18 '18 at 21:58
  • Yes, this is one of my previously consulted answers, but I still didn't make it work... – MorbidDesign Dec 19 '18 at 08:14

0 Answers0