Firebase - Check if it's the first time signing in

FiringBlanks Source

I need to check if the user is signing in for the first time, and initialize their account with extra properties (points, membership, accountCreationDate, etc) if true. My code currently only signs the user in. I'm following along with the documentation (https://firebase.google.com/docs/auth/web/google-signin) (Step 5).

angularfirebasefirebase-authenticationgoogle-cloud-firestore

Answers

answered 6 months ago Ti Wang #1

You can use the isNewUser field from addtionalUserInfo object.

answered 6 months ago FiringBlanks #2

Solution

I decided to just go my own way with this:

googleLogin() {
    const provider = new firebase.auth.GoogleAuthProvider();
    return this.oAuthLogin(provider);
}

private oAuthLogin(provider) {

  return this.afAuth.auth.signInWithPopup(provider).then((credential) => {

    const userRef: AngularFirestoreDocument<User> = this.afs.doc(`Users/${credential.user.uid}`);

    userRef.ref.get().then((doc) => {
      if(doc.exists){
        console.log('User exists!');
        this.updateUserData(credential.user);
      } else {
        console.log('User doesnt exist. Creating...');
        this.createUserData(credential.user);
      }
    })

  })

}

comments powered by Disqus