Firebase Auth, how to know new user signed up, rather than existing user sign in?

Viewed 10572

My use case is that I want to ask newly signed up users to enrich basic info like their names. So I was hoping to do it like:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    if (some indicator tells me it is newly signed up user)
        {redirect to a form to fill in more info}
  } else {
    // No user is signed in.
  }
});

I checked the doc, and could not find anything related to this...

Thanks for the help in advance.

3 Answers

You can check the sign-in methods the user has (if any). If there are none, it is a new user.

// Fetch sign in methods (if any)
Auth.auth().fetchSignInMethods(forEmail: userEmail!) { [self] signInMethodsArray, error in
        
    // Check for error and alert user accordingly
    if let error = error {
        // handle errors
    }
    // Email accepted.
    // Check if new or returning user.
    else {
            
        if (signInMethodsArray == nil) {
            // New User
                
        }
        else {
            // Returning User
                
        }
    }
}

This is Swift (iOS) code, but the concept is the same across languages.

Related