【问题标题】:Storage of firebase authentication tokenfirebase 身份验证令牌的存储
【发布时间】:2013-10-12 14:28:56
【问题描述】:

我有一个 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>

【问题讨论】:

    标签: authentication angularjs token firebase angularfire


    【解决方案1】:

    如果您使用的是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 会自动设置为用户详细信息。

    【讨论】:

    • 非常感谢我添加到 $scope 的身份验证功能不是必需的。
    猜你喜欢
    • 1970-01-01
    • 2020-06-26
    • 2020-12-16
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2022-07-16
    • 2021-01-08
    • 2021-09-23
    相关资源
    最近更新 更多