繁体   English   中英

firebase 函数模拟器:context.auth 未定义

[英]firebase functions emulator: context.auth is undefined

我们正在使用模拟器来创建一些本地测试环境。 我们遇到了 context.auth 总是未定义的问题。

谢谢!

const functions = require('firebase-functions');
const util = require('util');

exports.helloWorld = functions.https.onCall(async (data, context) => {
  console.log("helloWorld", util.inspect(context.auth));
  return {
    auth: context.auth
  };
});

这是脚本代码

 <script type="module">
    import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.8.4/firebase-app.js';
    import {getAuth,connectAuthEmulator, signInAnonymously } from 'https://www.gstatic.com/firebasejs/9.8.4/firebase-auth.js';
    import { getFunctions, connectFunctionsEmulator, httpsCallable } from 'https://www.gstatic.com/firebasejs/9.8.4/firebase-functions.js';
    initializeApp({
      projectId: 'demo-project1',
      apiKey: 'dummy',
    });
    document.addEventListener('DOMContentLoaded', async function () {
      const auth = getAuth();
      connectAuthEmulator(auth, "http://localhost:9099");
      const functions = getFunctions();
      connectFunctionsEmulator(functions, "localhost", 6001);
      auth.onAuthStateChanged(user => {
        console.log("USER CHANGED TO ", user);
        if (user) {
          callFunction();
        }
      });
      await signInAnonymously(auth);
    });

    async function callFunction() {
      const functions = getFunctions();
      const helloWorld = httpsCallable(functions, 'helloWorld');
      let result = await helloWorld({ text: "hello" });
      console.log(result.data);
    };
  </script>

完整示例也在 GH https://github.com/eran-swimm/test-emulators相关票证https://github.com/firebase/firebase-tools/issues/4690

问题是缺少安装 firebase-admin。 运行时,模拟器给了我这些警告。

The Cloud Functions emulator requires the module "firebase-admin" to be installed as a dependency. To fix this, run "npm install --save firebase-admin" in your functions directory.

i  functions: Your functions could not be parsed due to an issue with your node_modules (see above)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM