title: Redirecting user if PERMISSION_DENIED
body: <p>I want to enable people to login and deny access to particular states depending on the permissions set in the firebase rules which I have achieved successfully.</p>
  
  <p>I want to avoid the "page flash" redirection.</p>
  
  <p>I have followed the "Auth" state redirection code but I cannot pick up the PERMISSION_DENIED code in the object left by "return $firebaseObject(ref2);"</p>
  
  <pre><code>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");
                  }
              });
  }]);
  </code></pre>
  
  <p>This is my state  showing the resolve</p>
  
  <pre><code>.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();
          }
      }
  })
  </code></pre>
  
  <p>When my user is denied from the "events" node, they can't see the data which is great, but I would like them to redirect to the home state and maintain a logged in state.</p>
  
  <p>I placed a hack redirect in the "eventsCtrl" but it flashes before being redirected to the "home" state which is not what I want.</p>
  
  <p>MYEDIT - I placed this at the top of the eventsCtrl</p>
  
  <pre><code>        var refPermissionDeniedEvents = new Firebase("https://xxxxx.firebaseio.com/events");
          refPermissionDeniedEvents.on("value", function(snapshot) {
          }, function(err){
              $state.go("home", {}, { reload: true });
          });
  </code></pre>
  
  <p>After doing this, it enables a user to login and be authenticated and sit on the home page, the are denied access to the events page unless I change their permissions in the USERS node and on the permission rules are set on the EVENTS NODE.</p>
  
  <p>After doing this, I have achieved what I need.</p>