Wednesday, December 6, 2017

Firebase Authentication Trigger

If you want to familiar with what is firebase and firebase cloud functions before jumping into this article please visit the following links.

1) Firebase RealTime Database
2) Firebase Authentication Service
3) Why we need cloud functions
4) Introduction to cloud functions

We can trigger auth cloud functions that can run in response to user creation or deletion.

For ex: From your web or mobile app when ever there is a user signup you can invoke your auth trigger in the backend, which is a firebase cloud function, that will send a welcome email to the user. You don't need to duplicate sending welcome email code in your web and mobile apps. Common business logic can move to the server side.

Please checkout the following projects from github if you want to play around.

Web Client:

Firebase Functions:

First one is a frontend web app using which you can send request to your firebase backend.
Second one is your firebase cloud functions which will be triggered in specific conditions.

If you have issues setting up cloud function project please check the link Introduction to cloud functions above

** Following function from the web client will send a user registration request to firebase:

firebase.auth().signInWithEmailAndPassword(email, password)
                .then(function(result) {
                .catch(function(error) {
                    console.log('signIn error', error);

** Following cloud function in firebase will be invoked and performed few extra work and save it.

//Auth trigger
exports.createUserAccount = functions.auth.user().onCreate(event => {
  const uid =;
  const email =;
  const photoUrl = || '';
  const newUserRef = ref.child(`/users/${uid}`);

  return newUserRef.set({
    photoUrl: photoUrl,
    email: email

- The above auth trigger will be invoked every time there is a new user creation in firebase.
- In the above function you can see that its checking for photo url which should come as a input from client, if it is not present then trigger is adding a default photo url.
- Once the data is ready trigger is writing to the child node(/users/useruid/) of user pointing to user id.
- Firebase stores User data in its own place which we cant change but we can store our own data into Real time database.

For more information please visit:

Useful Read :

No comments:

Post a Comment