unclelim12
unclelim12

Reputation: 613

Change an existing $http request interceptor

In my angular app, I have a $http request interceptor called 'authInterceptor' that I create like this:

.factory('authInterceptor', function ($q, $window, EXT_API_BASE, $injector) {
return {
  request: function (config) {

    if (config.url.indexOf(EXT_API_BASE) !== -1){
      var Auth = $injector.get('Auth');
      if(Auth.user && Auth.user.token){
        config.headers.Authorization = 'Web ' + Auth.user.token;
      }
    }

    return config;
  }
}});

It gets registered in a .config():

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

As you see, my Authorization headers are bound to a Auth.user.token value. This value is available when my user is signed in.

Then the headers are sent for any calls to my api.

The problem I am facing is... when the user signs out in my angular app, the Authorization headers are still being sent even though I deleted Auth.user.token already.

On a hard refresh of the page, the Authorization headers then get removed completely.

How can I make sure 'authInterceptor' registers the change in token value when my user signs out?

Upvotes: 0

Views: 513

Answers (1)

unclelim12
unclelim12

Reputation: 613

Answering my own question. Made a newbie mistake of changing Auth.user object like this on sigh out:

Auth.user = {}

That created a new object and the requestInterceptor was still referencing the old object.

The correct way to do it is:

delete Auth.user.token

Upvotes: 0

Related Questions