[英]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.