How to pass session cookie via ajax with Traccar API

Victor Butler 5 years ago

Hi Anton,

May I ask you for advice on the following please - I can perform a session authentication using username and password and can successfully get a session cookie with a cURL function like this:

public static function login($email,$password){	
	$data='email='.$email.'&password='.$password;	
	return self::curl('/api/session','POST','',$data,array(self::$urlEncoded));
}

After that, I would like to use this session cookie to create a Web Socket connection and get the live device positions. I checked out the simple example here:
https://github.com/traccar/traccar-web/blob/master/web/simple/app.js.

In this code however, the authentication is done with a token and I'd like to use the session cookie that I already have. Is this possible to achieve?

I have in mind something like this (from app.js file):

ajax('GET', url + '/api/server', function(server) {
    //ajax('GET', url + '/api/session?token=' + token, function(user) { //don't use a token 
    ajax('GET', url + '/api/session, " 'Cookie': 'JSESSIONID' ", function(user) {  //but use the session cookie instead

Do you think this would be possible or the only option for WebSocket authentication is with token?

Anton Tananaev 5 years ago

If you already have the session, why do you want to call the session API again?

Victor Butler 5 years ago

Are you saying that if I already have a session I don't need this line of code at all?

ajax('GET', url + '/api/session?token=' + token, function(user) {

What should I replace it with? If I do this: ajax('GET', url + '/api/session', function(user) {

I get https://example.com/api/session 404 (Not Found)

Anton Tananaev 5 years ago

If you get 404, it means that cookie session is not persisted. You need to figure out why. Probably CORS issue or something like that.

Victor Butler 5 years ago

Question, if I do a login request first (for example on index.html) and only then I try to load the map on another page like map.html, is the session going to be kept?
I assume it should persist but still it doesn't (or at least not with the original app.js code.

Anton Tananaev 5 years ago

It is persisted for me.

Victor Butler 5 years ago

Ok, I will keep on troubleshooting. Can you confirm, just to be sure the code is correct, if I already have a session and I replace only this line:

ajax('GET', url + '/api/session?token=' + token, function(user) {

with this one in app.js:

ajax('GET', url + '/api/session', function(user) {

the session call should go trough successfully?

FYI, I have already setup <entry key='web.origin'>*</entry> in conf file.

Anton Tananaev 5 years ago

Correct.

Victor Butler 5 years ago

Hi Anton,

I found the issue and how to fix it, however I don't have enough expertise with session cookies to explain it properly.

So, in very basic terms, if authentication is done with php/cURL, this session cannot be also used for authentication on the traccar server. Therefore, a separate authentication is required either via token or via username/passwords.

Those are my two cents, I hope this will help the community. If anyone has more experience with session cookies feel free to jump in.