austbot
8/7/2015 - 3:53 AM

Require a login on a route easily.

Require a login on a route easily.

//Define your route
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/private', {
      templateUrl: 'privatePage.html',
      controller: 'PrivateCtrl as ctrl',
      requireLogin: true //Here is the extra attrbute
    });
  }])

// Where AuthService is your user / authentication service
.run(['$rootScope', '$location', 'authService', function ($rootScope, $location, coffeeApiService) {
    $rootScope.$on('$routeChangeStart', function (event, next, current) {
      //The magic is the $$route variable which contains the extra param you set above.
      if(next.$$route.requireLogin  && authService.loggedIn === false) $location.path('/login');
    });
}]);