Skip to content

Instantly share code, notes, and snippets.

@artgon
Created June 20, 2013 19:00
Show Gist options
  • Save artgon/5825602 to your computer and use it in GitHub Desktop.
Save artgon/5825602 to your computer and use it in GitHub Desktop.
Route description and auth enforcement
angular.module("crazyhorse").config(function ($routeProvider) {
'use strict';
$routeProvider.when('/login', {
templateUrl: 'users/login.html',
controller: 'LoginController',
resolve: {
organization: ['OrganizationService', function (OrganizationService) {
return OrganizationService.resolveOrganization();
}],
validLogin: ['SessionService', 'AuthenticationService', function (SessionService, AuthenticationService) {
return AuthenticationService.validateLogin().success(function (data) {
if (data.loggedIn) {
SessionService.redirectToDefault();
}
});
}]
}
});
// ...
});
angular.module("crazyhorse").run(function ($rootScope, $location, AuthenticationService, SessionService) {
'use strict';
var routesThatDontRequireAuth = ['/login'];
$rootScope.$on('$routeChangeStart', function (event, next, current) {
// if route requires auth and user is not logged in
if (!_(routesThatDontRequireAuth).contains($location.path()) && !AuthenticationService.isLoggedIn()) {
// remember the url they were going to
SessionService.interruptedUrl = $location.url();
// redirect them to login page
SessionService.redirectToLogin();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment