Last active
December 18, 2015 15:39
-
-
Save davemo/5806100 to your computer and use it in GitHub Desktop.
A way to handle auth redirects for rails + angular
This file contains 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("app", [])).tap (app) -> | |
app.config ['$httpProvider', ($httpProvider) -> | |
# csrf header | |
$httpProvider.defaults.headers.common["XSRF-TOKEN"] = "inject token here" | |
# all the requests are XHR! | |
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XmlHttpRequest' | |
# to handle 401s from rails | |
$httpProvider.responseInterceptors.push(cep.ng.interceptors.BroadcastSessionExpiredOn401) | |
] |
This file contains 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
def 'cep.ng.interceptors.BroadcastSessionExpiredOn401', | |
($rootScope, $q) -> | |
success = (response) -> response | |
error = (response) -> | |
if response.status is 401 | |
deferred = $q.defer() | |
req = | |
config: response.config | |
deferred: deferred | |
$rootScope.$broadcast('session:expired') | |
deferred.promise | |
$q.reject(response) | |
(promise) -> promise.then(success, error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment