Created
June 20, 2013 19:00
-
-
Save artgon/5825602 to your computer and use it in GitHub Desktop.
Route description and auth enforcement
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
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