We are using emulators to create some local test env. We get into problem that the context.auth is always undefined.
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
The problem was missing installation of 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.