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:
1) https://github.com/fullstacktechnos/firebase-frontend

Firebase Functions:
2) https://github.com/fullstacktechnos/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) {
                    console.log(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 = event.data.uid;
  const email = event.data.email;
  const photoUrl = event.data.photoUrl || 'http://lorempixel.com/400/200/';
  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.

**Note:
For more information please visit:
https://firebase.google.com/docs/functions/auth-events

Useful Read :
http://www.fullstacktechnos.com/2017/12/firebase-realtime-database-trigger.html
http://www.fullstacktechnos.com/2017/12/express-app-using-firebase-http-trigger.html

1 comment:

  1. Sports betting is anticipated to be the main segment out there market}. Based on devices, the market is categorized into desktop, mobile, and others (tablet, iPad, and so on.). When Insider spoke to Huang, he implied he wished to cease gambling and showed Insider an email he despatched to a gambling addiction charity to get help. The staff usually stay outside China in international locations including the Philippines or Cambodia, and are paid around $1000 a month, Ben Lee stated. "They need a virtual little military of younger Chinese youth to do the telemarketing," he added. 카지노 사이트 However, David Lee, a gambling lawyer at Lin & Partners, says the regulation "does not explicitly mention whether or not the Chinese regulation enforcement can take actions towards gambling operators outside China."

    ReplyDelete