2

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

  1. check if the user is logged in.
  2. wait till I receive a yes/no
  3. if not logged in. use auth.login
  4. 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.

4

3 回答 3

6

您不希望每个控制器都有一个 FirebaseAuthClient,但您确实希望在用户的身份验证状态发生变化时提醒所有控制器。

FirebaseAuthClient将为您处理会话存储。用户成功登录后,您只需要隐藏登录屏幕/按钮。

尝试这样的事情:

.service('myAuthService', ["$rootScope", function($rootScope) {
    var ref = new Firebase("https://kingpinapp.firebaseio.com");
    this.auth = new FirebaseAuthClient(ref, function(error, user) {
        if (user) {
            $rootScope.$emit("login", user);
        }
        else if (error) {
            $rootScope.$emit("loginError", error);
        }
        else {
            $rootScope.$emit("logout");
        }   
    });
}])
.controller('myCtrl', ["$scope", "$rootScope", "myAuthService", function($scope, $rootScope, myAuthService) {
    $scope.signin = function() {
        var user1 = $scope.cred.user;
        var pass1 = $scope.cred.password;

        myAuthService.auth.login('password', {
            email: user1,
            password: pass1,
            rememberMe: false
        });
    }
    // listen for user auth events
    $rootScope.$on("login", function(event, user) {
        // do login things
        $scope.user = user;
    })
    $rootScope.$on("loginError", function(event, error) {
        // tell the user about the error
    })
    $rootScope.$on("logout", function(event) {
        // do logout things
    })
}])
<button ng-show="user" ng-click="signin()">Sign in</button>
于 2013-05-10T19:35:10.297 回答
2

确保将 $q 作为依赖项包含在控制器中。在简单的情况下:

function MyController($scope, $q, angularFire) {
  $scope.signin = ...
}

或者,如果您使用的是“正确的”模块语法:

angular.module("MyModule", ["firebase"]).
controller("MyController", ["$scope", "$q", "angularFire", function($scope, $q, aF) {
  $scope.signin = ...
}]);

在此处了解有关角度依赖注入的更多信息:http: //docs.angularjs.org/guide/di,并查看https://gist.github.com/sbrekken/5151751以获取使用延迟的 Firebase 身份验证的示例。

于 2013-05-07T21:57:04.643 回答
0

我已经在这里发布了这个问题的答案FACTORY: get current user.id for Firebase Simple Login (Email / Password)

这是解决这个问题的一个非常可靠的解决方案,可能正是您正在寻找的。

于 2013-07-06T02:29:26.093 回答