javascriptfirebasevue.jsfirebase-authentication

Firebase - check if user created with Google Account is signing up or logging in?


I'm trying to create a web application, where you can log-in and register an account with a google account. I have managed to make it so they can log-in with the signInWithPopup(provider), but not sure how to Implement the sign-up. Any suggestions? or know of any functions in firebase i can use?


Solution

  • There aren't any separate methods to login and sign up when using Google Sign-In or any other provider. If a user with that credential exists then user will be logged in else a new user account will be created. You can then use additionalUserInfo.isNewUser property from the result received from signInWithPopUp method to check if the user is new.

    firebase.auth().signInWithPopup(provider).then(function (result) {
      const {additionalUserInfo: {isNewUser}} = result;
    
      console.log(isNewUser ? "This user just registered" : "Existing User")
    })
    

    For the new Modular SDK (V9.0.0+), the same can be written as:

    import { signInWithPopup, getAdditionalUserInfo } from "firebase/auth"
    
    const result = await signInWithPopup(auth, googleProvider);
    
    // Pass the UserCredential   
    const { isNewUser } = getAdditionalUserInfo(result)