Created
August 14, 2014 09:50
-
-
Save ilguzin/206f92f9471699a890e6 to your computer and use it in GitHub Desktop.
AngularJS. Resolve auth object before routing occurs. Allows to check user authentication before page is shown. The trick is to suppress new value into resolve array for each $routeProvider.when automatically (inside $rootScope.$on "$routeChangeStart")
This file contains hidden or 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
.run(['$rootScope', '$location', 'Auth', ($rootScope, $location, Auth) -> | |
$rootScope.$on "$routeChangeStart", (event, next, current) -> | |
redirectPath = $location.path() | |
console.debug "Route change started.", next, current, next.access, Auth.isLoggedIn() | |
_authorize = () -> | |
if next.access != undefined | |
if not Auth.isLoggedIn() | |
Auth.authorize().then( | |
(user) -> | |
if not user or not Auth.hasAccess(next.access, user) | |
Auth.logout() | |
$location.path('/login').search( 'redirectPath': redirectPath ) | |
user | |
) | |
else if not Auth.hasAccess(next.access) | |
Auth.logout() | |
$location.path('/login').search( 'redirectPath': redirectPath ) | |
next.resolve = angular.extend( next.resolve || {}, { | |
profile: _authorize | |
}); | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment