Home > Blockchain >  firebase - check if an user is already registered in database
firebase - check if an user is already registered in database

Time:05-19

I have a firebase app that is running on mobile devices and now I need to create the web version of it.

At the moment I have integrated firebase ui to give to users the ability to login. Since I need to show a wizard if the user isn't already registered, how I can check if user exists in the database so I can decide if show the wizard or not and then add it to the database?

CodePudding user response:

I consider you are using firebase auth and firestore, but this is the logic

firebase.auth().onAuthStateChanged(async (user)=> {
            if (user == null){
              //the user is not signed in, navigate to login screen
            }else{
            //the user is signed in, try to check if he exists in the database or register him
            //let say you are using firestore roo
              let user_ = await firebase.app().auth().currentUser
              firebase.firestore().collection("collections_of_users").doc(user_.uid).get().then(snap =>{
              if(snap.exists){
              //the user exists
              }else{
                //register him, and before make sure his email is verified by checking if emailVerified === true
                firebase.firestore().collection("collections_of_users").doc(user_.uid).set({user_mail: user_.email, user_name: "xxx"})
              }
              })
    
    
    }
    
    
       });

CodePudding user response:

It sounds like you're looking for the fetchSignInMethodsForEmail method, which returns the list of providers with which a specified email address has previously been signed in to Firebase.

The typical flow is:

  • Get the email address of the user
  • Call fetchSignInMethodsForEmail to get a list of providers
  • If the list is empty, go to your account creation flow
  • If the list has a single value, show the sign in screen for that provider
  • If the list has multiple values, show a screen where the user can pick from those providers
  • Related