Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase test auth cloud functions locally

exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
  console.log(user.uid);
  console.log(user.email);
  console.log(user.displayName);
});

exports.getUserInfo = functions.https.onCall(async (data, context) => {
  // get array of user IDs and returns information (from Users collection)
  const userIDs = data.userIDs;
  const result = [];
  const querySnapData = await admin.firestore().collection("Users").get();
  querySnapData.forEach((ele) => {
    if (userIDs.indexOf(ele.id) !== -1 && ele.id !== context.auth.uid) {
      result.push(ele.data());
    }
  });
  return { res: result };
});

I've got these two functions in my project - one is callable function and the other one is auth trigger functions.

So in my client app, I run

firebase.functions().useFunctionsEmulator('http://localhost:5001');

let getUserInfo = functions.httpsCallable('getUserInfo');
getUserInfo({userIDs: data}).then(res => doSomething);

And to run the cloud functions locally

firebase emulators:start

But it says

functions[sendWelcomeEmail]: function ignored because the auth emulator does not exist or is not running.

So in the client App, getUserInfo works pretty well but can't trigger onCreate.

But I was not able to find any document about auth emulator.

Any link/article/video or answer is appreciated.

like image 840
Kid Avatar asked Jul 05 '20 01:07

Kid


People also ask

Can you use Firebase locally?

The Firebase Local Emulator Suite is a set of advanced tools for developers looking to build and test apps locally using Cloud Firestore, Realtime Database, Cloud Storage, Authentication, Cloud Functions, Pub/Sub, Firebase Hosting and Firebase Extensions.


2 Answers

The Firebase Emulator Suite currently Cloud Firestore, Realtime Database, Cloud Functions, and Cloud Pub/Sub. It does not yet emulate Firebase Authentication APIs. So any auth calls you make will be executed against the real project that is associated with the emulators.

This also means that your functions.auth.user().onCreate((user) => { Cloud Function will not be triggered in the emulators at the moment. You'll have to deploy it to the servers to test this trigger.

To learn when an auth emulator is available, I recommend keeping an eye on Firebase's release notes, and on the main documentation page for the emulator suite that lists the supported products. You can also follow along more closely on Github, either in the commits, or in this feature request.

like image 130
Frank van Puffelen Avatar answered Oct 19 '22 17:10

Frank van Puffelen


firebase recently released the Authentication Emulator, you can check it via the release notes here: https://firebase.google.com/support/releases#october_26_2020, and further guide here: https://firebase.google.com/docs/emulator-suite/connect_auth

like image 41
favs Avatar answered Oct 19 '22 18:10

favs