Authorization cookie is not being set

ghost377 years ago

Hi, I'm working on application in AngularJS, which I want to integrate with Traccar API. I have a problem with creating session. I'm sending a POST request with valid credentials, it gives 200 status code, but authorization cookie is not being set. Without any headers modifications, API returns 401 on other requests. I've read in internet that it can be a problem with Http headers, but when I'm trying to configure headers in many different ways, each time Chrome Console says somethin like:

Access to XMLHttpRequest at 'http://localhost:8082/api/session' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field Credentials is not allowed by Access-Control-Allow-Headers in preflight response.

I've never get it working yet.
What can I do to fix it ? Thanks for help

Anton Tananaev7 years ago

Have you configured CORS?

ghost377 years ago

I guess that I should add some line in configuration file. I found this line in other posts:

< entry key='web.origin'>*< /entry>

but I can't find it in documentation on site

https://www.traccar.org/configuration-file/

Should I add it for the newest Traccar version?
I guess that I also need to configure my webapp. Can I find somewhere any example how to do it correctly ?

Anton Tananaev7 years ago

The configuration parameter is still available in the latest release:

https://github.com/traccar/traccar/blob/master/src/org/traccar/api/CorsResponseFilter.java#L48

ghost377 years ago

Ok, thanks for quick help

ghost377 years ago

I've added web.origin line in default.xml file and created session with request headers like this:

this.headers = this.headers.set('Accept', '*/*');
this.headers = this.headers.set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

I'm getting Set-Cookie header in response, but it's not being set for other requests. Still getting 401 for them

ghost377 years ago

Ok i found a solution. I had to add withCredentials: true option in HttpOptions, thanks for help Anton ;)