【问题标题】:Redirecting user if PERMISSION_DENIED如果 PERMISSION_DENIED 重定向用户
【发布时间】:2016-02-19 23:03:57
【问题描述】:

我想让人们根据我成功实现的 firebase 规则中设置的权限登录和拒绝访问特定状态。

我想避免“页面闪烁”重定向。

我遵循了“Auth”状态重定向代码,但我无法在“return $firebaseObject(ref2);”留下的对象中获取 PERMISSION_DENIED 代码

myapp.factory("Auth", function($firebaseAuth) {
  var ref = new Firebase("https://xxxx.firebaseio.com/");
  return $firebaseAuth(ref);
});


//myedit, got rid of this factory
///myapp.factory("notLive", function($firebaseObject) {
///  var ref2 = new Firebase("https://xxxx.firebaseio.com/events");
///  return $firebaseObject(ref2);
///});


// for ui-router
myapp.run(["$rootScope", "$state", function($rootScope, $state) {
                $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error, code) {
                if (error === "AUTH_REQUIRED") {
                    $state.go("home");
                }

                    console.log(code); // returns undefined :(
                ///myedit - changed this
                ///if (code === "PERMISSION_DENIED") { 
                if (error.code === "PERMISSION_DENIED") { 
                    $state.go("home");
                }
            });
}]);

这是我的决心

.state("events", {
    url: "/events",
    templateUrl: "tpl/events.html",
    controller: "eventsCtrl",
    resolve: {
        currentAuth: function(Auth) {
            console.log(Auth.$requireAuth());
            return Auth.$requireAuth();
        },
        ///myedit - changed this
        ///notLive: function(notLive) {
        ///   console.log(notLive.$loaded());
        ///    return notLive.$loaded();
        ///}

        notLive: function($firebaseObject) {
            var refEvents = new Firebase("https://xxxxx.firebaseio.com/events");
            return $firebaseObject(refEvents).$loaded();
        }
    }
})

当我的用户被“事件”节点拒绝时,他们看不到很棒的数据,但我希望他们重定向到主状态并保持登录状态。

我在“eventsCtrl”中放置了一个黑客重定向,但它在被重定向到我不想要的“home”状态之前闪烁。

MYEDIT - 我把它放在事件的顶部Ctrl

        var refPermissionDeniedEvents = new Firebase("https://xxxxx.firebaseio.com/events");
        refPermissionDeniedEvents.on("value", function(snapshot) {
        }, function(err){
            $state.go("home", {}, { reload: true });
        });

完成此操作后,它使用户能够登录并通过身份验证并坐在主页上,除非我在 USERS 节点中更改他们的权限并且在 EVENTS 上设置权限规则,否则他们将被拒绝访问事件页面节点。

这样做之后,我已经达到了我所需要的。

【问题讨论】:

  • 我改变了一些东西并设法让它工作,见 myedit

标签: angularjs angular-ui-router firebase angularfire firebase-security


【解决方案1】:

您可以通过这种方式“装饰”有角度的 routeProvider:

(function (angular) {
  "use strict";

  var securedRoutes = [];

  angular.module('myApp.security', ['ngRoute', 'Auth', 'myApp.config'])

    .config(['$routeProvider', function ($routeProvider) {

      $routeProvider.whenAuthenticated = function (path, route) {

        securedRoutes.push(path); // store all secured routes for use with authRequired() below
        route.resolve = route.resolve || {};

        route.resolve.user = ['Auth', function (Auth) {
          return Auth.$requireAuth(); //fetch auth info
        }];

        $routeProvider.when(path, route);
        return this;

      };

    }])

  /**
   * Apply some route security. Any route's resolve method can reject the promise with
   * { authRequired: true } to force a redirect. This method enforces that and also watches
   * for changes in auth status which might require us to navigate away from a path
   * that we can no longer view.
   */
    .run(['$rootScope', '$location', 'Auth', 'loginRedirectPath',

      function ($rootScope, $location, Auth, loginRedirectPath) {
        // watch for login status changes and redirect if appropriate
        Auth.$onAuth(check);

        // some of our routes may reject resolve promises with the special {authRequired: true} error
        // this redirects to the login page whenever that is encountered
        $rootScope.$on("$routeChangeError", function (e, next, prev, err) {
          if (err === "AUTH_REQUIRED") {
            $location.path(loginRedirectPath);
          }
        });

        function check(user) {
          if (!user && authRequired($location.path())) {
            //console.log('check failed', user, $location.path()); //debug
            $location.path(loginRedirectPath);
          } 
        }

        function authRequired(path) {
          return securedRoutes.indexOf(path) !== -1;
        }
      }
    ]);

})(angular);

您将loginRedirectPath 设置为默认路径(登录页面或其他内容)以在用户未通过身份验证时重定向

这旨在检查用户是否仅通过身份验证,但您可以在function check(user) 中进行额外检查(权限等)

【讨论】:

    【解决方案2】:

    我认为“代码”无效。这里没看到: https://github.com/angular-ui/ui-router/wiki 在 $stateChangeError 部分。

    编辑:不是肯定的,但您可能需要在 catch 语句中获取 PERMISSION_DENIED 错误。

    $loaded().then(function(){}).catch(function(error){console.log(error)})
    

    【讨论】:

    • 我确实尝试了“错误”,但它返回了“auth”错误,而不是“permission_denied”错误
    • 见评论。您是否尝试过捕获与此类似的 PERMISSION_DENIED?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2021-01-16
    • 2013-01-06
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多