Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set $httpProvider default headers after user authentification

I am considering to add a default header for the $http service, which value is an access token that will be generated after user authentification.

module.config('$routeProvider', '$locationProvider', '$httpProvider'){
    $httpProvider.defaults.headers.post['XSRF-AUTH'] = 
        "some accessToken to be generated later"; 
}

Problem is, the config() block is applied when Angular bootstraps its core components. Is there a way to alter $ĥttpProvider dynamically ?

like image 792
Mehdi Chibouni Avatar asked Apr 17 '14 03:04

Mehdi Chibouni


1 Answers

You can change the default header through the $http object during runtime instead of the $httpProvider. For example you can do the following outside of a config block:

$http.defaults.headers.post['XSRF-AUTH'] = "access token";

Check out the $http api docs for more details http://docs.angularjs.org/api/ng/service/$http#setting-http-headers.

like image 193
Leo Avatar answered Oct 04 '22 03:10

Leo