Created
March 27, 2013 02:17
-
-
Save giosakti/5251029 to your computer and use it in GitHub Desktop.
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
app.run ($rootScope, Session, AuthTokenHandler, $location) -> | |
# Watch Session signedIn variable and reasign rootScope session variables | |
# accordingly. | |
$rootScope.$watch (-> | |
Session.signedIn | |
), () -> | |
$rootScope.currentUser = Session.currentUser | |
$rootScope.signedIn = Session.signedIn | |
$rootScope.signedOut = Session.signedOut | |
AuthTokenHandler.set(Session.userSession.auth_token) | |
# On event:loginConfirmed, put session data and redirect to root path | |
$rootScope.$on('event:loginConfirmed', (events, sessionData) -> | |
Session.putSessionData(sessionData) | |
$location.path "/" | |
) | |
# On event:logoutConfirmed, remove session data and redirect to root path | |
$rootScope.$on('event:logoutConfirmed', (events, sessionData) -> | |
Session.removeSessionData() | |
$location.path "/" | |
) | |
# On event:loginRequired, redirect to login path | |
$rootScope.$on('event:loginRequired', (events) -> | |
$location.path "/login" | |
) | |
# On event:unauthorized, redirect to root path | |
$rootScope.$on('event:unauthorized', (events) -> | |
$location.path "/" | |
) | |
app.config ($httpProvider) -> | |
# Create interceptor for handling 401 & 403 request | |
interceptor = ($rootScope, $q) -> | |
success = (response) -> | |
response | |
error = (response) -> | |
if response.status is 401 | |
deferred = $q.defer() | |
$rootScope.$broadcast "event:loginRequired" | |
return deferred.promise | |
else if response.status is 403 | |
deferred = $q.defer() | |
$rootScope.$broadcast "event:unauthorized" | |
return deferred.promise | |
$q.reject response | |
(promise) -> | |
promise.then success, error | |
# Register interceptor for handling 401 & 403 request | |
$httpProvider.responseInterceptors.push interceptor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment