0

I'm just trying to get this to work:

 .....
   .when('/channel/:id/:slug',{
            templateUrl:'views/channel/index.html',
            controller:'Channel',
            publicAccess:true,
            sessionAccess:true
          })
   .....
    app.controller('Channel', ['$scope','$routeParams', function ($scope,$routeParams) {

    }]);


    app.run(function($rootScope, $location, $route) {

      var routesOpenToSession = [];

      angular.forEach($route.routes, function(route, path) {
        console.log(path);
        console.log(route);
        route.sessionAccess && (routesOpenToSession.push(path));
      });

      $rootScope.$on('$routeChangeStart', function(event, nextLoc, currentLoc) {

        var closedToSession = (-1 === routesOpenToSession.indexOf($location.path()));

        if(closedToSession && $rootScope.session.id_user) {
          $location.path('/');
        }
      });
    });

why i can't access the page via site.com/channel/9/my-slug also if $rootScope.session.id_user exists and sessionAccess:true ?

i get redirected to / , while any other static url are ok using sessionAccess:true for example channel/staticparam is ok but with dynamic params it won't work

this is the console log result :

enter image description here

9
  • 'channel' != 'channels' Commented Feb 25, 2014 at 22:28
  • there is a typo in url. on first look Commented Feb 25, 2014 at 22:30
  • sorry yeah a typo i also added the main cause of the redirect ! Commented Feb 25, 2014 at 22:37
  • not taking to account a lot of BL logic, do you have same issue, when just do a try to open a route? are you using html5mode,which ignores hashes # in url? Commented Feb 25, 2014 at 22:38
  • 1
    I start believe that closedToSession is always true, + session id object is true. plnkr.co/edit/B4glcHZwht48wezJ5NiE?p=preview I have created a sample. maybe it will help you to understand the issue Commented Feb 25, 2014 at 23:15

1 Answer 1

1

fixed sorry for the stupid question:

/*Not logged redirects*/
app.run(['$rootScope','$location','$route', function ($rootScope, $location,$route) {

   var routesOpenToPublic = [];

    angular.forEach($route.routes, function (route, path) {

      if(route.publicAccess){ routesOpenToPublic.push(route.regexp); }

    });

    $rootScope.$on('$routeChangeStart', function (event, nextLoc, currentLoc) {

     var next_url_regexp = nextLoc.$$route.regexp;
    //redirect for not logged users users
     if(routesOpenToPublic.indexOf(next_url_regexp) < 0){

      $location.path('/auth/login');

     }

    });
}]);
/*Logged redirects*/
app.run(['$rootScope','$location','$route', function ($rootScope, $location, $route) {

  if($rootScope.session && $rootScope.session.id_user){

    var routesOpenToSession = [];

    angular.forEach($route.routes, function (route, path) {

      if(route.sessionAccess){ routesOpenToSession.push( route.regexp);}

    });

    $rootScope.$on('$routeChangeStart', function (event, nextLoc, currentLoc) {

     var next_url_regexp = nextLoc.$$route.regexp;
    //redirect for not allowed session users
     if(routesOpenToSession.indexOf(next_url_regexp) < 0){

      $location.path('/');

     }

    });
  }

 }]);

i needed to check the route regexp and not the static url path

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.