I'm attempting to check for a user before saving them in the firebase users table, but when I go to check using the .once() firebase method, the function always returns false...
When I console.log inside the function, it logs correctly, however it never returns true.
Auth Represents a basic Firebase Auth Factory
var newUser = function(id) {
ref.child('users').child(id).once('value', function(snapshot) {
console.log(snapshot.val() === null);
return (snapshot.val() === null);
});
};
Auth.$onAuth(function(authData) {
console.log(Boolean(newUser(authData.uid)));
if (authData && !!(newUser(authData.uid))) { // add user if first time login
$scope.authData = authData;
ref.child('users').child(authData.uid).$save({
provider: authData.uid,
name: getName(authData),
blah: 'blah'
});
$state.go('main.home');
} else if (authData) {
$state.go('main.home');
} else {
console.log('Not Logged In');
}
});