Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Auth, how to know new user signed up, rather than existing user sign in?

Tags:

My use case is that I want to ask newly signed up users to enrich basic info like their names. So I was hoping to do it like:

firebase.auth().onAuthStateChanged(function(user) {   if (user) {     // User is signed in.     if (some indicator tells me it is newly signed up user)         {redirect to a form to fill in more info}   } else {     // No user is signed in.   } }); 

I checked the doc, and could not find anything related to this...

Thanks for the help in advance.

like image 965
LeonTan Avatar asked Aug 06 '17 07:08

LeonTan


People also ask

How do I know if an account is signed in Firebase authentication?

You can easily detect if the user is logged or not by executing: var user = firebase. auth().


2 Answers

Since version 4.6.0: https://firebase.google.com/support/release-notes/js#4.6.0 You can get if a user is new or existing in 2 ways:

  1. If you are getting back a UserCredential result, check result.additionalUserInfo.isNewUser

  2. Check firebase.auth().currentUser.metadata.creationTime === firebase.auth().currentUser.metadata.lastSignInTime

Previously you had to do that on your own and keep track of the user using Firebase Realtime Database. When a user signs in, you check if a user with the specified uid exists in the database or not. If the user was not found, it is a new user, you can then add the user to the database. If the user is already in the database then this is a returning existing user. Here is an example in iOS.

Handing Firebase + Facebook login process

Example for using result.additionalUserInfo.isNewUser:

firebase.auth().signInWithPopup(provider).then((result) => {   console.log(result.additionalUserInfo.isNewUser); }); 
like image 77
bojeil Avatar answered Oct 16 '22 13:10

bojeil


One thing you can do is do things in the callback function of the signup function, the signup function do return a promise. You can do something like this:

firebase.auth().createUserWithEmailAndPassword(email, password) .then(function(user) {     //I believe the user variable here is the same as firebase.auth().currentUser     //take the user to some form you want them to fill }) .catch(function(error) {   // Handle Errors here.   var errorCode = error.code;   var errorMessage = error.message;   // ... }); 

enter image description here

However, I don't really recommend doing it this way because the client side code can be unreliable. Think about what if a user suddenly disconnect before they can fill the form. Their data will be incomplete in your database. So if you do it this way, do set a flag in your user's profile when they submit the form so that you know who filled detailed information and who didn't.

Another better way to do this is using firebase cloud functions. You can have code like this in your cloud functions. Cloud functions are written in node.js so you don't need to spend time on another language.

exports.someoneSignedUp = functions.auth.user().onCreate(event => {   // you can send them a cloud function to lead them to the detail information form   //or you can send them an welcome email which will also lead them to where you want them to fill detailed information }); 

This way is much better because you can safely assume that your cloud functions server will never be down or compromised. For more information about cloud functions you can refer to their doc: https://firebase.google.com/docs/functions/auth-events

like image 24
Tianhao Zhou Avatar answered Oct 16 '22 12:10

Tianhao Zhou