I am implementing token based authentication with AngularJS. The token is created on server and returned to client. After authentication, the token will be added to header of every rest call. I created a authInterceptor:
ristoreApp.factory('authInterceptor', function ($rootScope, $q, $window) {
return {
request: function (config) {
config.headers = config.headers || {};
if ($window.localStorage.getItem("access_token")) {
config.headers.Authorization = 'Bearer ' + $window.localStorage.getItem("access_token");
}
return config;
},
response: function (response) {
if (response.status === 401) {
// handle the case where the user is not authenticated
}
return response || $q.when(response);
}
};
});
Then injected it in my config.js as follows:
ristoreApp
.config(function ($httpProvider, authInterceptor, $routeProvider) {
$httpProvider.interceptors.push('authInterceptor');
$routeProvider
.......
})
However I got the following error:
Failed to instantiate module ristoreApp due to: Unknown provider: authInterceptor
What is wrong with my way to inject the interceptor?