Created
August 7, 2015 03:53
-
-
Save austbot/fd80f615fa092ffeb88b to your computer and use it in GitHub Desktop.
Require a login on a route easily.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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