The main task is to find out from which user the data comes, perhaps the approach that I use in this example will not work.

I try to establish session for the user from one site in another, there is site1 written on php and site2 written on nodejs. C site1 I send ajax request to the url http: // site2: 3000 / send , session is established there.

$.ajax({ type: 'GET', url: "http://site2:3000/send", dataType: 'html', success: function (data) { $.ajax({ type: 'GET', url: "http://site2:3000/test-send", dataType: 'html', success: function (data) { alert(data); } }); } }); 

But as soon as I turn to the following URL http: // site2: 3000 / test-send, the session that was set up for http: // site2: 3000 / send is no more. The nodejs code itself is next

 router.get('/send', function(req, res, next) { req.session.test_req = "red"; console.log(req.session.test_req); // После обращения через site1 сессия в консоли есть res.end();}); router.get('/test-send', function(req, res, next) { console.log(req.session.test_req); // После обращения через site1 сессии здесь уже нет res.end();}); 

If you access site2 directly through the browser, then the sessions are set correctly and saved.
Is it possible to solve the problem in such a way that to create a session for the user by sending an ajax request via another service or is some other solution necessary?

  • Sessions cannot be transferred between origins. - Nazar Kalytiuk
  • And how can I find out from which user the request was sent? - Vayas
  • In this case, the problem is on the browser side. Some browsers do not accept cookies from third-party sites (and your site2:3000 from their point of view is a third-party site) in order to protect the user from being tracked. - Pavel Mayorov
  • In other words, browsers intentionally prevent you from solving your problem - “find out from which user the data is coming,” and this will not change. - Pavel Mayorov
  • You can try to generate a token in / send, which will then be checked in / test-send, but this will require changing the client code. Or it is necessary to return not html, and json - or it will be necessary to add processing of the headings. - Pavel Mayorov

0