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.