Can Cloud Functions for Firebase execute on user login?

Viewed 7605

I see how execute a Cloud Function on user account creation:

exports.myFunction = functions.auth.user().onCreate(event => { 

But I need my function to execute when the user logs in. Is there a onLogin trigger?

And can someone with 1500 points create a tag for firebase-cloud-functions?

5 Answers

There's no event for login, because only the client side can define exactly when a login happens. Different clients may define this in different ways. If you need to trigger something on login, figure out when that point is in your app, then trigger it from the client via a database or HTTP function.

This worked in the controller:

firebase.auth().onAuthStateChanged(function(user) { // this runs on login
    if (user) { // user is signed in
      console.log("User signed in!");
      $scope.authData = user;
      firebase.database().ref('userLoginEvent').update({'user': user.uid}); // update Firebase database to trigger Cloud Function
    } // end if user is signed in
    else { // User is signed out
      console.log("User signed out.");
    }
  }); // end onAuthStateChanged

And this is the trigger in the Cloud Function:

exports.getWatsonToken = functions.database.ref('userLoginEvent').onUpdate(event => { // authentication trigger when user logs in

I made a location in Firebase Database called userLoginEvent.

One confusing bit is that in the functions console it's /userLoginEvent but in your code you must leave out the slash. enter image description here

You can create your own analytics event, like login and used it as the trigger for your cloud function.

Then in your app, when the user successfully authenticate you use firebase analytics to send an event with the name you defined, like login

exports.sendCouponOnPurchase = functions.analytics.event('login').onLog((event) => {
  const user = event.user;
  const uid = user.userId; // The user ID set via the setUserId API.


});

You can trigger an https onCall firebase cloud function on login

ex: This is your login button trigger function which calls an https onCall function after authenticating the user.

_login() {
            firebase
                .auth()
                .signInWithEmailAndPassword(this.state.email, this.state.password)
                .then(function (user) {
                    var addMessage = firebase.functions().httpsCallable('myCloudFunctionName');
                    addMessage("whatever variable I want to pass")
                    .catch(error => {
                        console.log("I triggered because of an error in addMessage firebase function " + error)
                    )}
                }).catch(error => {
                    console.log(error);
                });
        }

There is also another way you can do this inside Google Cloud if you enable Identity Platform for a project. Then you can follow this guide:

https://cloud.google.com/functions/docs/calling/logging

And trigger cloud functions for any of these Firebase Authentication events:

https://cloud.google.com/identity-platform/docs/activity-logging?authuser=1&_ga=2.226566175.-360767162.1535709791#logged_operations

The only problem I've just noticed is that the logs generated for Sign In event do not include the firebase app id or anything to determine which client the user logged in on which is really annoying as this was the main reason we needed to do this!

Related