firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.02k stars 934 forks source link

Emulator: auth functions not triggered #2847

Closed jonadeline closed 3 years ago

jonadeline commented 3 years ago

It seems that the firebase functions based on auth events (in my case functions.auth.user().onCreate) are not triggered within the emulator suite. In Emulator UI i'm able to see that my auth function is initialized correctly but nothing happens when a user is created (from client side with auth.createUserWithEmailAndPassword() method) image

I set the required env variables (FIREBASE_AUTH_EMULATOR_HOST and GCLOUD_PROJEC) when launching the emulator

My dependencies are up to date : firebase-tools 8.16.2 firebase-admin: 9.4.1 firebase-functions: 3.11.0

Note : regular firebase functions are triggered properly in the emulator.

samtstern commented 3 years ago

@jonadeline have you called firebase.auth().useEmulator("http://localhost:9099") on the client side before attempting to create the user? Do you see the created user in the Emulator UI?

jonadeline commented 3 years ago

Hi @samtstern, yep the user creation is working like a charm in the emulator. I can see it in the Emulator UI.

samtstern commented 3 years ago

@jonadeline I am able to reproduce this as well:

functions/index.js

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

exports.onNewUser = functions.auth.user().onCreate((user, ctx) => {
  console.log(JSON.stringify(user));
  return true;
});

public/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Welcome to Firebase Hosting</title>

    <script defer src="/__/firebase/8.1.1/firebase-app.js"></script>
    <script defer src="/__/firebase/8.1.1/firebase-auth.js"></script>
    <script defer src="/__/firebase/init.js?useEmulator=true"></script>
  </head>
  <body>
    <div id="message">
      <h2>Welcome</h2>
    </div>
    <script>
      document.addEventListener('DOMContentLoaded', () => {
        const username = `user${new Date().getTime()}@example.com`;
        const password = 'password';

        firebase.auth().createUserWithEmailAndPassword(username, password)
          .then((res) => {
            console.log(res);
          })
          .catch((err) => {
            console.warn(err);
          });
      });
    </script>
  </body>
</html>

I see the users created in the Emulators UI but there are no function logs reporting that the auth function ran:

$ firebase emulators:start
i  emulators: Starting emulators: auth, functions, hosting
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: firestore, database, pubsub
⚠  Your requested "node" version "12" doesn't match your global version "10"
i  hosting: Serving hosting files from: public
✔  hosting: Local server: http://localhost:5000
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/private/var/folders/xl/6lkrzp7j07581mw8_4dlt3b000643s/T/tmp.E1IMu4ZF/functions" for Cloud Functions...
✔  functions[onNewUser]: auth function initialized.

┌────────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! View status and logs at localhost:4000 │
└────────────────────────────────────────────────────────────────┘

┌────────────────┬────────────────┬──────────────────────────┐
│ Emulator       │ Host:Port      │ View in Emulator UI      │
├────────────────┼────────────────┼──────────────────────────┤
│ Authentication │ localhost:9099 │ localhost:4000/auth      │
├────────────────┼────────────────┼──────────────────────────┤
│ Functions      │ localhost:5001 │ localhost:4000/functions │
├────────────────┼────────────────┼──────────────────────────┤
│ Hosting        │ localhost:5000 │ n/a                      │
└────────────────┴────────────────┴──────────────────────────┘
  Other reserved ports: 4400, 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.

i  hosting: 127.0.0.1 - - [24/Nov/2020:18:13:52 +0000] "GET / HTTP/1.1" 200 906 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
i  hosting: 127.0.0.1 - - [24/Nov/2020:18:13:52 +0000] "GET /__/firebase/init.js?useEmulator=true HTTP/1.1" 200 - "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
i  hosting: 127.0.0.1 - - [24/Nov/2020:18:13:52 +0000] "GET /__/firebase/8.1.1/firebase-app.js HTTP/1.1" 200 - "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
i  hosting: 127.0.0.1 - - [24/Nov/2020:18:13:52 +0000] "GET /__/firebase/8.1.1/firebase-auth.js HTTP/1.1" 200 - "http://localhost:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
i  hosting: 127.0.0.1 - - [24/Nov/2020:18:13:53 +0000] "GET /__/firebase/8.1.1/firebase-app.js.map HTTP/1.1" 200 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
i  hosting: 127.0.0.1 - - [24/Nov/2020:18:13:53 +0000] "GET /__/firebase/8.1.1/firebase-auth.js.map HTTP/1.1" 200 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"

Even when creating a user directly in the UI, no functions run.

samtstern commented 3 years ago

@abeisgoat @yuchenshi this seems like a big bug, do we have tests covering this? Am I missing something?

yuchenshi commented 3 years ago

We don't have integration tests with client SDKs and let's add those as a first step. (We do have them for users created by admin SDKs). After that, we should be able to reproduce it and catch regressions in the future.

NMVW commented 3 years ago

@jonadeline @samtstern i am also able to reproduce this bug locally.

In the interim, connecting auth directly to a test gcp project environment to test OAuth flows work, but not ideal for testing integration with rest of firebase emulator services.

samtstern commented 3 years ago

@jonadeline @NMVW I just tried this again and so did @yuchenshi and we're no longer able to reproduce the issue:

i  functions: Beginning execution of "onNewUser"
>  {"uid":"wuYlyJFPerRdYgqOqA6yboKHJpxN","email":"user1606337128082@example.com","emailVerified":false,"displayName":null,"photoURL":null,"phoneNumber":null,"disabled":false,"passwordHash":null,"passwordSalt":null,"tokensValidAfterTime":null,"metadata":{"creationTime":"1606337128401","lastSignInTime":"1606337128400"},"customClaims":{},"providerData":[{"providerId":"password","email":"user1606337128082@example.com","federatedId":"user1606337128082@example.com","rawId":"user1606337128082@example.com"}]}
i  functions: Finished "onNewUser" in ~1s

Are you sure your functions emulator is running with the same project ID that you're using with the Web SDK?

jonadeline commented 3 years ago

Yes, on my side I set the GCLOUD_PROJECT_ID env var with the same value used by the Web SDK client side.

Here is how I start the emulator : FIREBASE_AUTH_EMULATOR_HOST='localhost:9099' GCLOUD_PROJECT='project-id' firebase emulators:start

But the connexion between the emulator and the client side looks good as regular firebase functions are triggered correctly in the emulator on my environnement.

johanbuys commented 3 years ago

I am having the same issue.

Other Functions are working "https.onCall" Debugger jumps to breakpoints etc, but auth.user().onCreate is not firing, don't know if the emulator suite emulates this behaviour but auth.user().onCreate also does not fire when creating a user from the emulator dashboard.

yuchenshi commented 3 years ago

@jonadeline @johanbuys would you please do me a favor and try firebase emulators:start --project foobar instead, where foobar matches the project ID used on the client? This forces the firebase command to use a certain project ID. And please remove the environment variables -- those don't do much.

kartikwatwani commented 3 years ago

I created this repository to demonstrate another issue but I am also facing the issue that the functions.auth.user().onCreate function is not triggered when the user is created from the angular app while using firebase emulators.

johanbuys commented 3 years ago

@jonadeline @johanbuys would you please do me a favor and try firebase emulators:start --project foobar instead, where foobar matches the project ID used on the client? This forces the firebase command to use a certain project ID. And please remove the environment variables -- those don't do much.

Hi just tested with: firebase emulators:start --inspect-functions --import=./firestoreData --export-on-exit --project xxxx No change, one thing I would like to mention, don't think it will make a difference will test it out today, is that i am running in a remote dev environment and forwarding ports to my local machine, so environment for the server is running Unbuntu and local is running on mac.

Edit: I also use a non default region: functions.region('europe-west2').auth.user().onCreate

jonadeline commented 3 years ago

@jonadeline @johanbuys would you please do me a favor and try firebase emulators:start --project foobar instead, where foobar matches the project ID used on the client? This forces the firebase command to use a certain project ID. And please remove the environment variables -- those don't do much.

Not better on my side. 😞

samtstern commented 3 years ago

@itsmeneartou I was able to clone and run your reproduction, thanks for providing it! When I signed in to the Angular app I can confirm that no Cloud Function triggered, but I also didn't see any user in my Emulator UI so I don't think the frontend is talking to the Auth Emulator at all. I suspect this is because you're using FirebaseUI, which does not yet have support for the Firebase Auth emulator.

samtstern commented 3 years ago

@johanbuys does removing the region change anything?

NMVW commented 3 years ago

@itsmeneartou I was able to clone and run your reproduction, thanks for providing it! When I signed in to the Angular app I can confirm that no Cloud Function triggered, but I also didn't see any user in my Emulator UI so I don't think the frontend is talking to the Auth Emulator at all. I suspect this is because you're using FirebaseUI, which does not yet have support for the Firebase Auth emulator.

Confirming that in my environment I am also relying on FirebaseUI for auth flows

johanbuys commented 3 years ago

I have changed region and no effect. I too have been using firebaseUI.

However I am adding users from the emulator frontend now, would this not trigger the function? (The docs state that creating a user from the Firebase console will trigger the function, not sure if this behaviour is emulated.)

jonadeline commented 3 years ago

fyi I am not using firebaseUI

samtstern commented 3 years ago

@johanbuys @jonadeline yes if you add a user through the Emulator UI and don't see your functions triggered that's a bug, and that's probably the easiest place for us to focus going forward since it removes variables of how people use their web frontend (FirebaseUI, etc)

johanbuys commented 3 years ago

@johanbuys @jonadeline yes if you add a user through the Emulator UI and don't see your functions triggered that's a bug, and that's probably the easiest place for us to focus going forward since it removes variables of how people use their web frontend (FirebaseUI, etc)

@samtstern yes that's the behaviour I am experiencing, create user from ui, function not firing. I have a debugger attached does not hit the bp.

jonadeline commented 3 years ago

@samtstern yes that's the behaviour I am experiencing, create user from ui, function not firing. I have a debugger attached does not hit the bp.

same for me

jjalonso commented 3 years ago

I have found this issue today, im using emulator since few weeks back and ir works fine but today i had to implement my first auth event (onCreate) and it doesnt get triggered. no region.

johanbuys commented 3 years ago

Hi, An update from my side, after updating firebase-tools the onCreate function is firing again.

Also one thing to note is that firebase-ui is not compatible with the auth emulator the firebase-ui team is still working on getting it to work. When using Firebase-ui it will send all comms to the configured project directly.

NB: So when you use firebase-ui all auth requests will bypass the emulator (even if useEmulator() is called) and thus the local onCreate functions will not get called.

see here: https://github.com/firebase/firebaseui-web/issues/778#issuecomment-721535405

samtstern commented 3 years ago

Hey everyone it's been a while since we last got a report of this issue. Reading through the thread again it sounds like some combination of the following will fix it:

I'm going to close this one, but if anyone sees this issue again just let me know and we can reopen it.