0

Im making an AngularJS Application and Im trying to insert JSONwebtoken into the headers, but I can't figure out what's wrong with my code. Can you guys help me out?

My application config:

application.config(['$routeProvider','$httpProvider', '$locationProvider', function($routeProvider, $locationProvider, $localStorage, $window, $httpProvider) {

  $httpProvider.interceptors.push('AuthInterceptors');

}]);

And my AuthInterceptors factory:

application.factory("AuthInterceptors", function(AuthToken) {
  var authInterceptorsFactory = {};

    authInterceptorsFactory.request = function(config) {

      var token = AuthToken.getToken();

      if(token) config.headers['x-access-token'] = token;

      return config;
    };

  return authInterceptorsFactory;
});

Thanks guys!

2 Answers 2

1

Your order of dependencies is not correct in your function. You have to keep the same order of dependencies in your function and in your annotations,

application.config(['$routeProvider','$httpProvider', '$locationProvider', function($routeProvider,$httpProvider, $locationProvider) {

  $httpProvider.interceptors.push('AuthInterceptors');

}]);
Sign up to request clarification or add additional context in comments.

Comments

0

From the Docs:

Inline Array Annotation

When using this type of annotation, take care to keep the annotation array in sync with the parameters in the function declaration.

— AngularJS Developer Guide - Dependence Injection

This problem can be avoided by using Implicit Annotation. Tools like ng-annotate let you use implicit dependency annotations in your app and automatically add inline array annotations prior to minifying.

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.