avian
avian

Reputation: 1753

How to set $httpProvider default headers from my app config in AngularJS?

I'm trying to set my

$httpProvider.defaults.headers.common['X-CSRF-Token'] = $cookie.auth_token; 

in my .config section of my app, but it doesn't seem like I can access the document/cookies just yet? Is there a better place to set this?

I'm doing this because I'm storing my users auth_token in a cookie so they don't need to login every time they use my mobile app.

Thanks!

Upvotes: 3

Views: 6252

Answers (1)

Stewie
Stewie

Reputation: 60406

As described in $http docs:

... Angular provides a mechanism to counter XSRF. When performing XHR requests, the $http service reads a token from a cookie called XSRF-TOKEN and sets it as the HTTP header X-XSRF-TOKEN.

... To take advantage of this, your server needs to set a token in a JavaScript readable session cookie called XSRF-TOKEN on the first HTTP GET request

So if you set your CSRF token in cookie name XSRF-TOKEN then no adjustments are needed on Angular side. and your code should work as is.

Upvotes: 1

Related Questions