How to differentiate signin and signup user in firebase using google auth?

I am using firebase to authenticate user. If new user signin using google/facebook provider, it creates a user and automatically sign in. After user signin, how to differentiate between signIn and signUp?

var provider = new firebase.auth.GoogleAuthProvider();

firebase.auth().signInWithPopup(provider).then(function(result) {
  var token = result.credential.accessToken;
  var user = result.user;
  // if new user i want to redirect to some other page.
  // if user already exist, i want to redirect to home page
}).catch(function(error) {
  // Handle Errors here.
});

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The UserCredential object you get as result has an additionalUserInfo member that has an isNewUser property.

So result.additionalUserInfo.isNewUser will be true the first time the user signs in, and false afterwards.

Method 2

use getAdditionalUserInfo

var provider = new firebase.auth.GoogleAuthProvider();

firebase
  .auth()
  .signInWithPopup(provider)
  .then(function (result) {
    var token = result.credential.accessToken;
    var user = result.user;
    const additionalUserInfo = getAdditionalUserInfo(userCredential);
    if (additionalUserInfo.isNewUser) {
      // if new user i want to redirect to some other page.
    } else {
      // if user already exist, i want to redirect to home page
    }
  })
  .catch(function (error) {
    // Handle Errors here.
  });


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x