调用Cloud Function HTTP触发器的CORS策略错误

时间:2018-04-11 00:52:58

标签: javascript firebase cors google-cloud-functions

我使用CORS支持编写并部署了 Firebase Cloud功能

const cors = require('cors')({
    origin: true
  });
...
exports.test = functions.https.onRequest((req, res) => {

    cors(req, res, () => {
        const idToken = req.query.idToken;
        admin.auth().verifyIdToken(idToken)
           .then((decoded) => {
               var uid = decoded.uid;
               return res.status(200).send(uid);
         })
          .catch((err) => res.status(401).send(err));
    });

});

我使用Axios包从我的React应用程序调用HTTP Trigger:

   firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
        // Send token to your backend via HTTPS
        // ...
        console.log(idToken);
        axios.get('https://XXX.cloudfunctions.net/test?idToken='+idToken)
        .then(response => console.log(response));

      }).catch(function(error) {
        // Handle error
      });

不幸的是,在本地服务器上运行应用程序时,我仍然收到错误:

Failed to load...Redirect From..to..
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

PS。我使用了报告的程序here

1 个答案:

答案 0 :(得分:-1)

我找到了答案here

public static class Extensions
{
    public static void Deconstruct(this string[] array, out string s1, out string s2)
    {
        s1 = array[0];
        s2 = array[1];
    }
}