0

In angular App, I have 2 pages, each of them for according to user's privileged level. for that how can I redirect templates from routerby using resolve or without?

what would be the correct way?

here is what I am looking for :

$routeProvider.when('/secretpage' , {
        templateUrl: null,        
        resolve:{
            "check":function(accessFac,$location){  
                if(accessFac.checkPermission()){    
//check if the user has permission -- This should happen before the page loads
return this.template: "templates/secretpage.html"

                } else {

                    this.template: "templates/secretlesspage.html"
                }
            }
        }
})
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

2 Answers2

1

A better and clean way is to have 2 routes, say /secretpage and /secretless and redirect based on the privilege using below route configuration:

$routeProvider
  .when('/secretpage' ,{
    templateUrl: "templates/secretpage.html",        
    resolve:{
        "check":function(accessFac,$location){  
            if(!accessFac.checkPermission()){    
               $location.path('/secretless')
            }
        }
    }
  })
  .when('/secretless', {
    templateUrl: "templates/secretlesspage.html",
    resolve: {
      "check":function(accessFac,$location){  
        if(accessFac.checkPermission()){    
           $location.path('/secret')
        }
      }
    }

  })
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
0

The common approach to secure the pages is to use the $routeChangeStart event broadcasted before the route changes:

angular.module('myApp', [])
.run(function($rootScope, $location, accessFac) {
    $rootScope.$on('$routeChangeStart',
        function(evt, next, curr) {
            if (!accessFac.checkPermission()) {
                $location.path('/login'); // redirect
            }
    })
});

https://stackoverflow.com/a/11542936/2430054

Community
  • 1
  • 1
Alexander Kravets
  • 4,245
  • 1
  • 16
  • 15