Skip to content

Instantly share code, notes, and snippets.

@austbot
Created August 7, 2015 03:53
Show Gist options
  • Save austbot/fd80f615fa092ffeb88b to your computer and use it in GitHub Desktop.
Save austbot/fd80f615fa092ffeb88b to your computer and use it in GitHub Desktop.
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');
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment