【问题标题】:how to call firebase callable functions from localhost?如何从 localhost 调用 firebase 可调用函数?
【发布时间】:2020-04-27 01:27:24
【问题描述】:

我收到此错误:

每当调用此云函数时:

      const makeAdmin = firebase.functions().httpsCallable("makeAdmin");
      makeAdmin({
        make: "admin"
      })
        .then(response => {
          console.log(response);
        })
        .catch(err => console.error(err));

功能是:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
const auth = admin.auth();

exports.makeAdmin = functions.https.onCall(async (data, context) => {
  try {
    const email = context.auth.token.email || null;
    const user = await auth.getUserByEmail(email);
    await auth.setCustomUserClaims(user.uid, {
      admin: true
    });
    return {
      message: "admin added successfully"
    };
  } catch (error) {
    return error;
  }
});

我尝试使用 cors 模块但没有用。

【问题讨论】:

  • 你也部署了这个功能吗?
  • 是的。部署成功
  • 尝试使用ngrok 创建一个公共端点并对其进行测试,这样您就可以确定这是一个本地主机问题。
  • 你能在 firebase web 控制台中看到你的函数吗?你能检查你的函数的日志吗?
  • 这是您的index.js 文件吗?如果没有,您是否从主文件中导入了此代码?

标签: javascript firebase vue.js google-cloud-functions


【解决方案1】:

自 2020 年 1 月 15 日起,新功能默认需要身份验证。

解决方案是在 Google Cloud Console 的 Cloud Functions 页面中手动将 Cloud Functions Invoker 权限添加到 allUsers 用户。

  1. 转到谷歌云控制台:https://console.cloud.google.com/functions/

  2. 单击要授予访问权限的函数旁边的复选框。

  3. 单击右上角的显示信息面板以显示权限选项卡。

  4. 点击添加成员。

  5. 在新成员字段中,输入 allUsers。

  6. 从选择角色下拉菜单中选择角色 Cloud Functions > Cloud Functions Invoker。

  7. 点击保存。

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

【讨论】:

    【解决方案2】:

    我知道应该做什么,因为我遇到过同样的事情

    对于本地主机::无需安装cors(最好卸载) 参考下面修改你的functions/index.js

    exports.yourFuncation = functions.https.onRequest((request, response) => {
    response.set('Access-Control-Allow-Origin', "*")
    /*Your code and response*/})
    

    对于生产:使用相同的代码,但在 cors 中使用你的生产 url 而不是 localhost

    并且应该修改你的firefunction的firebase.json如下

    {
    "functions": {
        "predeploy": [
            "npm --prefix \"$RESOURCE_DIR\" run lint"
        ],
        "headers": [{
            "key": "Access-Control-Allow-Origin",
            "value": "https://production.com/*"
        }]
    }}
    

    您还需要在托管下的 firebase.json 中添加 cors 值

    "rewrites": [{
    "source": "**",
    "destination": "/index.html",
    "headers": [{
        "key": "Access-Control-Allow-Origin",
        "value": "https://us-central1-projectname.cloudfunctions.net*"
    }]}]}
    

    根据情况进行修改,如果遇到问题发表评论

    【讨论】:

      【解决方案3】:

      您需要启用cors,才能执行该请求:

      首先安装包:

      npm install cors
      

      然后导入:

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

      【讨论】:

      • 是的,我试过了。 const functions = require("firebase-functions"); const admin = require("firebase-admin"); const cors = require("cors")({ origin: true }); admin.initializeApp(); const db = admin.firestore(); const auth = admin.auth(); exports.makeAdmin = functions.https.onCall(async (data, context) => { try { const uid = context.auth.uid; await auth.setCustomUserClaims(uid, { admin: true }); cors(req, res, () => { res.send({ message: "admin added successfully" }); }); } catch (error) { return error; } });
      猜你喜欢
      • 2022-01-25
      • 2018-09-03
      • 1970-01-01
      • 2020-05-17
      • 2021-08-09
      • 2018-10-21
      • 2021-02-10
      • 1970-01-01
      • 2022-11-09
      相关资源
      最近更新 更多