简体   繁体   English

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

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

We are using emulators to create some local test env.我们正在使用模拟器来创建一些本地测试环境。 We get into problem that the context.auth is always undefined.我们遇到了 context.auth 总是未定义的问题。

Thanks!谢谢!

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
  };
});

This is the script code这是脚本代码

 <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>

The full example is also in GH https://github.com/eran-swimm/test-emulators Relevant ticket https://github.com/firebase/firebase-tools/issues/4690完整示例也在 GH https://github.com/eran-swimm/test-emulators相关票证https://github.com/firebase/firebase-tools/issues/4690

The problem was missing installation of firebase-admin.问题是缺少安装 firebase-admin。 When running, the emulator gave me these warnings.运行时,模拟器给了我这些警告。

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.

相关问题 Firebase函数context.auth和context.authType未定义 - Firebase function context.auth and context.authType undefined firebase云功能 - https.onCall(...)可以使用Context.Auth吗? - firebase cloud functions - https.onCall(…) can Context.Auth be used? 我是否需要在 firebase 云函数的 context.auth object 上使用 verifyIdToken? - Do I need to use verifyIdToken on the context.auth object in firebase cloud functions? Firebase HTTPS 可调用 function context.auth 在与自定义令牌一起使用时始终为 Z37A6259CC0C1DAE29Z0A76 - Firebase HTTPS callable function context.auth is always null when used with custom token auth 为什么 context.auth 没有出现在 Firebase 函数 onCreate 文档触发器的 context:EventContext 参数中? - Why isn't context.auth appearing in the context:EventContext parameter on the Firebase function onCreate document trigger? Firebase 用户的 context.auth 仍然存在 allAuthenticatedUsers 使用 UNAUTHENTICATED 保护 Google 功能错误 - Firebase user's context.auth is present still allAuthenticatedUsers secured Google function error with UNAUTHENTICATED JS,firebase.auth() 在全局上下文中不起作用,但在函数内部起作用? - firebase.Auth 不是函数 - JS, firebase.auth() doesn't work in global context but works inside the functions? - firebase.Auth is not a function 某些功能显示为未定义的 firebase/auth v9 - Some functions appear as undefined firebase/auth v9 Firebase auth Emulator 因 IDP 失败 - Firebase auth Emulator fails with IDP 如何连接到 Firebase Auth 模拟器 - How to connect to Firebase Auth emulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM