【问题标题】:persistent authentication states Firebase持久身份验证状态 Firebase
【发布时间】:2013-11-06 19:58:29
【问题描述】:

假设我已将写入规则配置为特定的email

{
  "rules": {
    ".read": true,
    ".write": "auth.email == 'example@example.com'"
  }
}

我使用angularFireAuth 服务登录:

.controller('loginCtrl', ["$scope", "$rootScope", "angularFireAuth", function($scope, $rootScope, angularFireAuth) {

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

    $scope.cred = {};

    $scope.signin = function() {

        angularFireAuth.login('password', {
            email: $scope.cred.user,
            password: $scope.cred.password,
            rememberMe: true
        });
    }
}]);

我有一个 newCtrl 用于将新项目添加到我的收藏:

.controller('newCtrl', ["$scope", "$rootScope", "angularFireCollection", function($scope, $rootScope, angularFireCollection) {

    var ref = new Firebase("https://test.firebaseio.com/");
    $scope.items = angularFireCollection(ref);
}]);

当我在我看来调用items.add() 时:

<input type="text" ng-model="item.name" />
<input type="text" ng-model="item.desc" />
<button ng-click="items.add(item)">submit</button>

即使通过loginCtrl成功登录后,我也会收到Not Authorized的回复

  • 我如何创建在我的整个应用程序中登录后的持久身份验证状态?
  • 注销后,如何删除整个应用程序的持久身份验证状态?

【问题讨论】:

  • 您能发布您的安全规则吗?
  • 您使用的是正确的 Firebase 网址吗?这个seems to work给我。
  • 很抱歉,如果没有身份验证凭据将无济于事,请使用 example@example.com 作为电子邮件地址和 example 作为密码。

标签: angularjs firebase angularfire firebase-security


【解决方案1】:

您的身份验证状态是基于会话的,不依赖于您的控制器范围,因此您的应用程序中已经有了“持久身份验证状态”。

您的代码肯定有其他问题会产生该错误,但就您的问题而言,您可以看到一个工作示例here,使用example@example.com 作为电子邮件,example 作为密码.

我也匹配了您的安全规则,因此如果您使用密码 testtest@example.com 登录,您将无法创建 items

【讨论】:

  • 感谢您抽出宝贵时间整理这些内容。我假设会话在多个选项卡中是持久的。我将跟进一个不同的问题,因为我不想更改当前问题的目标帖子。但是,如果您在同一个选项卡上,那么您是对的,这个示例绝对可以工作。
  • 是的,如果它有帮助,我认为会话 持久的,但身份验证 事件 是本地的。似乎我们需要一个 /.info/authenticated 引用来管理身份验证,就像管理 presence 一样。
【解决方案2】:

据我所知,angularFireAuth 从 $rootscope 广播登录事件。你可以设置事情发生.$on('angularFireAuth:login',function(event){...}),或.$on('angularFireAuth:logout',function(event){...}),或.$on('angularFireAuth:error',function(event,error){...})。考虑到这一点,您可能需要以下内容:

.controller('newCtrl', ["$scope", "$rootScope", "angularFireCollection", function($scope, $rootScope, angularFireCollection) {
    var ref = new Firebase("https://test.firebaseio.com/");
    $scope.items = angularFireCollection(ref);
    $scope.$on('angularFireAuth:login', function(){
        $scope.addItem=function(item){
            $scope.items.add(item);
        };
    });
}]);

.. 这应该确保在函数分配给 $scope 之前发生用户身份验证事件。

【讨论】:

  • 或@hiattp 所说的任何内容。他非常乐于助人。
猜你喜欢
  • 2019-09-20
  • 1970-01-01
  • 2018-10-31
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2019-07-04
相关资源
最近更新 更多