【问题标题】:firebase callable functions returning CORS error and not being called from clientfirebase 可调用函数返回 CORS 错误且未从客户端调用
【发布时间】:2020-03-26 20:46:51
【问题描述】:

我使用 firebase 函数已经有一段时间了,所有函数的部署都非常顺利。突然之间,部署的任何新功能都停止工作,来自客户端的任何调用都会返回 CORS 错误。如果我检查 firebase 仪表板中的函数列表,我看不到被调用的函数,这与如果函数根本不存在时我所期望的相似。

我现在只是在尝试一个简单的基本功能,如下所示:

exports.createSession = functions.region('europe-west2').https.onCall(async (data, context) => {
  return ({status: 200})
});

在前端,我正在做一个简单的调用,如下所示:

const createSessionFunction = functions.httpsCallable('createSession');
const response = await createSessionFunction({});

本周之前创建和部署的所有其他功能都运行良好。没有任何新功能。

我得到的错误如下:

Access to fetch at 'https://europe-west2-xxxxxxx.cloudfunctions.net/createSession' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
index.cjs.js:614 POST https://europe-west2-xxxxxxxz.cloudfunctions.net/createSession net::ERR_FAILED

我在 firebase GUI 上的函数列表显示该函数确实存在:

createSession - Request - https://europe-west2-xxxxxxxx.cloudfunctions.net/createSession-europe-west2-Node.js 8 -256 MB - 60s

但是日志显示,当我尝试测试它时,它从未从客户端调用,这意味着客户端可能根本没有检测到这个函数。

我尝试了以下步骤,但没有成功:

  1. 删除并重新部署函数
  2. 重命名函数并重新部署
  3. 在不同的应用程序(开发/测试等)上部署相同的新功能

有什么想法吗?

【问题讨论】:

  • 你什么时候部署这个功能的?因为今年年初新部署的功能上的一些权限发生了变化。另外,预检获取传递给您的应用程序的状态代码是什么?

标签: firebase google-cloud-functions


【解决方案1】:

通过授予我的所有功能公共访问权限,在谷歌云仪表板上解决了这个问题。默认权限已从公共更改为私有。

https://cloud.google.com/functions/docs/securing/managing-access-iam#allowing_unauthenticated_function_invocation

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2020-07-07
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多