1

我有一个 Firebase AngularJS 应用程序,我正在使用它FirebaseSimpleLogin来执行身份验证,但我在理解如何在我的应用程序中持久保存登录用户时遇到了一些麻烦。每次我登录并刷新页面时,似乎我不再经过身份验证。我知道登录后会生成一个令牌,用于对用户进行身份验证,但我是否需要在每个页面上使用此令牌对他们进行身份验证,如果是这样,这是否会在我的应用程序和 firebase 之间产生任何类型的开销?

我的应用程序执行登录/验证的部分

var ref = new Firebase("https://xxx.firebaseio.com/");
angularFireAuth.initialize(ref, {scope: $scope, name: "user", path:"/login"});

    // Used to authenticate user at a later time
$scope.auth = function(token, callback)
{

    ref.auth(token, function(error, data) {

        if(error) 
        {
            console.log("Login Failed!", error);
        } 
        else 
        {
            $scope.$apply(function()
            {
                $scope.user = data.auth;
            }); 
        }

        if (typeof callback === 'function')
        {
            callback(error,data);
        }

    });
};


    // Login to fetch authentication token to be used by $scope.auth()
$scope.login = function()
{   
    angularFireAuth.login("password", { 
        email: $scope.credentials.email,
        password: $scope.credentials.password
    });
};


    // On login stores user within $scope
$scope.$on("angularFireAuth:login", function(evt, user) {
    $scope.$apply(function()
    {
                 // Is it possible to save this user accross my application not just in this $scope
             $scope.user = user; 

    });
});

这是捕获电子邮件/密码组合的标记

<div class="container-single" center>
               <h3>Sign in</h3>
               <input type="text" ng-model="credentials.email" placeholder="Username" />
               <input type="password" ng-model="credentials.password" placeholder="Password" />
               <input type="checkbox" id="checkbox-1" name="checkbox-1" /> <label>Remember my password</label>
               <button ng-click="login()">Sign in</button>
           </div>
4

1 回答 1

2

如果您使用的是angularFireAuth,那么您不需要自己进行任何身份验证。只需检查该$scope.user值以查看用户是否在每个视图中都已登录。

var ref = new Firebase("https://xxx.firebaseio.com/");
angularFireAuth.initialize(ref, {scope: $scope, name: "user", path:"/login"});

$scope.login = function() {   
  angularFireAuth.login("password", { 
    email: $scope.credentials.email,
    password: $scope.credentials.password
  });
};

登录成功后,$scope.user自动设置为用户详细信息。

于 2013-10-08T18:55:35.690 回答