Skip to content

Instantly share code, notes, and snippets.

@ilguzin
Created August 14, 2014 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilguzin/206f92f9471699a890e6 to your computer and use it in GitHub Desktop.
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")
.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