angularfirebasefirebase-authenticationgoogle-cloud-firestore

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


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).


Solution

  • 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);
          }
        })
    
      })
    
    }