Last active
February 29, 2016 13:13
-
-
Save ansariabr/76bf8411243462486872 to your computer and use it in GitHub Desktop.
Interceptor for token based authentication
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
app.factory('authInterceptor', ['$q', function ($q) { | |
return { | |
request: function (config) { | |
config.headers = config.headers || {}; | |
if (config.headers.skipAuthorization === false) { | |
var token = localStorage.getItem('authenticationToken'); | |
if (token != null) { | |
config.headers.Authorization = token; | |
} | |
} | |
return config; | |
}, | |
response: function (response) { | |
if (response.headers("Authorization") != undefined || response.headers("Authorization") != '') { | |
localStorage.setItem('authenticationToken', response.headers("Authorization")); | |
} | |
return response; | |
}, | |
responseError: function (rejection) { | |
if (rejection.status === "401") { | |
localStorage.removeItem('authenticationToken'); | |
} | |
return $q.reject(rejection); | |
} | |
}; | |
} ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment