I am communicating from angular app to server side for data. The server creates a token and sends it into the header. I am looking for ways to intercept the response using Restangular and save it in the localStorage. I haven't been successful.
I was doing something like this in my app .config
RestangularProvider.addResponseInterceptor('HttpInterceptor');
and then in the HttpInterceptor I was calling the Storage to store the Header. But I am getting several errors and couldn't figure out this.
Thanks in advance!
Alright after much of a research, I finally found a solution that worked for me.
So in my app.js, I included this:
.config(function ($stateProvider, $urlRouterProvider, RestangularProvider, $httpProvider) {
$httpProvider.interceptors.push('AuthInterceptor');
}
And then I the AuthInterceptor service to do the header extraction.
angular.module('myApp')
.factory('AuthInterceptor', AuthenInterceptor);
AuthenInterceptor.$inject = ['$localStorage'];
function AuthenInterceptor($localStorage) {
return {
response: function (response) {
var authToken = response.headers('You Token Name');
if (authToken) {
$localStorage.userToken = authToken;
}
return response;
}
};
}
Hope it helps someone with similar problem. Thanks!