Daniel Torres Laserna
Daniel Torres Laserna

Reputation: 424

Firebase: How to recognize the first signup using an authentication provider

thanks for coming,

I am implementing Firebase authentication with Facebook and Twitter, I have a special need after the first registration in the user. We are creating a referal system and I need to keep a token and know if it is the first to log in to add points to the user that refers him.

I can easy separate both methods with email and password but i cannot do it with social provider.

signInRegular(email, password) {
    const credential = firebase.auth.EmailAuthProvider.credential(email, password);
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
}


emailSignUp(email: string, password: string) {
    return this.afAuth.auth.createUserWithEmailAndPassword(email, password).then(user => {
      this.userDetails = user;
      console.log(this.userDetails);
    }).catch(error => error);
}

The problem I have is that Firebase brings a unique method to log in and register.

signInWithFacebook() {
  this.auth.signInWithFacebook().then((res) => {
    console.log("Authenticated with Facebook")
  }).catch((err) => console.log(err));
}

I am planning to do it through the database and store if the users have a created account or not.

however, I would like to find a way to avoid making a query to the database

If you hace any suggestion or interest, everything is appreciated!

Hands up 😁👍🏼

Upvotes: 0

Views: 363

Answers (2)

Daniel Torres Laserna
Daniel Torres Laserna

Reputation: 424

Thanks to @bojeil he found this post

Stack overflow answer

Firebase auth did provide an indicator telling if the user is new or not.

firebase.auth().signInWithPopup(provider)
  .then(function(result) {
    console.log(result.additionalUserInfo.isNewUser);
  })
  .catch(function(error) {
    // Handle error.
  });

This is the Firebase documentation that refers to this feature.

Upvotes: 1

Ronnie Smith
Ronnie Smith

Reputation: 18595

You must either keep a record of the referral in your database/data store. Pass a unique key to the referer who will then pass it to the new user. Add a referral key input to your new user signup flow to collect the referral key.

As part of recording new users in your data store, query the referralKeys collection for a matching key (which will have the uid of the original referrer as a property)

Upvotes: 1

Related Questions