0

i´m trying to create a login with Google and facebook in my web application, the idea is login the user with google or facebook, check if the uid of the user is on the realtime database and the redirect the user.

checkRegisterUser: function(user){

      var register_user,
      register = false;

      app.references.user_ref.on('value', function(snapshot) {
        register_user = snapshot.val();
        console.log(register_user);
      });
      $.each(register_user, function(index, val){

        if(user.uid === index){
          register = true;
          return;
        }
      })
      console.log(register);
      if(!register){
        firebase.auth().signOut();
        $('#login').modal('hide');
        $('#modal_register').modal('show');
        return false;
      }
      $(window).attr('location', app.urlGallery());
    }


google_login: function(){

      var provider = new firebase.auth.GoogleAuthProvider();
      firebase.auth().signInWithPopup(provider).then(function(result) {
        // This gives you a Google Access Token. You can use it to access the Google API.
        var token = result.credential.accessToken;
        // The signed-in user info.
        var user = result.user;
        app.checkRegisterUser(user);
        // firebase.auth().signInWithRedirect(provider);
      }).catch(function(error) {
        console.log(error);
      });

    },

After Login with google or facebook i send user object the function checkRegisterUser() loop the array of user to see if the uid is already on the realtime database, if the user uid is already on the realtime database change to true the register variable and redirect, my problem is that always the first time if the user exist give me false and only when i try a second time give true.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Miguel Frias
  • 2,544
  • 8
  • 32
  • 53

1 Answers1

1

I faced similar issues on iOS due to the asyncronous nature of Firebase. Not 100% on this, but give it a try:

Logic - This function

app.references.user_ref.on('value', function(snapshot) {
        register_user = snapshot.val();
        console.log(register_user);
});

is asyncronous so we don't really know when it will return a value. Thus the loop can't execute until this call is returned. However you check if(!register) which can happen before the query returns a result. Therefore, since the value of register has not been updated this block will execute.

Secondly looping over all of your users to check if they are in the database is not efficent and certainly scales poorly. I recommend you change how you are storing your users to something like this so you can immediately check if they exist in the database.

users: {
    uid: {
        name: "Bart Simpson"
        email: "Bart@simpsons.com"
    }
    uid2: {
        ....
        ....
    }
}

This structure will allow you to make use of the if snapshot.exists(). This will tell you immediately if the user is in the database or not and allow you to perform your desired actions accordingly.

Using the above structure your query would look like this.

var userUID = uid; 
var ref = new Firebase('https://SampleChat.firebaseIO-demo.com/users/' + userUID);
ref.on('value', function(snapshot) {
   if (snapshot.exists())
      alert ("exist");
   else
      alert ("not exist");
});

You can read more about checking if a value exists in Firebase here.

DoesData
  • 6,594
  • 3
  • 39
  • 62