Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulator: auth functions not triggered #2847

Closed
jonadeline opened this issue Nov 21, 2020 · 24 comments
Closed

Emulator: auth functions not triggered #2847

jonadeline opened this issue Nov 21, 2020 · 24 comments

Comments

@jonadeline
Copy link

jonadeline commented Nov 21, 2020

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.

@jonadeline jonadeline changed the title Firebase auth functions not triggered Emulator: Firebase auth functions not triggered Nov 21, 2020
@jonadeline jonadeline changed the title Emulator: Firebase auth functions not triggered Emulator: auth functions not triggered Nov 21, 2020
@samtstern
Copy link
Contributor

@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
Copy link
Author

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

@google-oss-bot google-oss-bot added Needs: Attention and removed Needs: Author Feedback Issues awaiting author feedback labels Nov 24, 2020
@samtstern
Copy link
Contributor

samtstern commented Nov 24, 2020

@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
Copy link
Contributor

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

@yuchenshi
Copy link
Member

yuchenshi commented Nov 24, 2020

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

NMVW commented Nov 25, 2020

@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
Copy link
Contributor

samtstern commented Nov 25, 2020

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

@samtstern samtstern added the Needs: Author Feedback Issues awaiting author feedback label Nov 25, 2020
@jonadeline
Copy link
Author

jonadeline commented Nov 26, 2020

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.

@google-oss-bot google-oss-bot added Needs: Attention and removed Needs: Author Feedback Issues awaiting author feedback labels Nov 26, 2020
@johanbuys
Copy link

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
Copy link
Member

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

@yuchenshi yuchenshi added Needs: Author Feedback Issues awaiting author feedback and removed Needs: Attention labels Nov 30, 2020
@kartikwatwani
Copy link

kartikwatwani commented Dec 1, 2020

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

johanbuys commented Dec 1, 2020

@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
Copy link
Author

@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. 😞

@google-oss-bot google-oss-bot removed the Needs: Author Feedback Issues awaiting author feedback label Dec 1, 2020
@samtstern
Copy link
Contributor

@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
Copy link
Contributor

@johanbuys does removing the region change anything?

@NMVW
Copy link

NMVW commented Dec 2, 2020

@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
Copy link

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
Copy link
Author

fyi I am not using firebaseUI

@samtstern
Copy link
Contributor

@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
Copy link

@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
Copy link
Author

@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
Copy link

jjalonso commented Dec 8, 2020

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

johanbuys commented Dec 22, 2020

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: firebase/firebaseui-web#778 (comment)

@samtstern
Copy link
Contributor

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:

  • Updating firebase-tools
  • Updating FirebaseUI
  • Making sure to match Project IDs

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants