Been playing around with firebase and angularjs and just trying to put some little things together. I have the auth working now in a controller with this function called on the sign in button click:
$scope.signin = function(){
var user1 = $scope.cred.user;
var pass1 = $scope.cred.password;
var ref = new Firebase("https://kingpinapp.firebaseio.com");
var auth = new FirebaseAuthClient(ref, function(error, user) {
if (user) {
// user authenticated with Firebase
console.log(user);
} else if (error) {
// an error occurred authenticating the user
console.log(error);
} else {
// user is logged out
}
});
auth.login('password', {
email: user1,
password: pass1,
rememberMe: false
});
console.log("tracer");
}
Now this is great and works fine. But it seems to work in a async manner for example my console.log("tracer") returns before the user object of the auth.login. I know I probably need to work with promises to get this done and tried doing the following:
var defer = $q.defer();
defer.auth
.then(function() {
auth.login('password', {
email: user1,
password: pass1,
rememberMe: false
});
})
.then(function() {
console.log("tracer");
})
But i'm receiving a $q is not defined after declaring it in the controller module. So what I'm trying to do is
- check if the user is logged in.
- wait till I receive a yes/no
- if not logged in. use auth.login
- else user user logged in do some other things
I thought of putting the auth.login function in the else of the variable auth but that doesn't seem like it would work. Just trying to figure out the proper logic in understanding how to get this to work.