1

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?

1
  • Can you post your detail code? It will be better if you add any fiddle/plnkr. Commented May 11, 2016 at 3:23

1 Answer 1

2

this Failed to Instantiate module happens when you have NOT defined

ristoreApp

in your Routing file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.