Question

I have a older system in php, so , i'm writing a new one with Node.js with Express framework, in some place with the Node.js site i need log into the php site (the sites use the same credentials).

Basically i have a button in some place in mi view (in Express) that when is pressed, should take some user credentials from some place, login into php site with this credentials (without any forms) and redirect to the php system already logged in on the browser.

I'm trying to follow this answer

So, here is the code, that execute when button is pressed.

app.post('/loginphp', function(req, res) {

  var data = request.post(
    'http://somesite.somedomain.com/Users/login', {
      form:{
       'data[User][email]': 'someuser@somedomain.com',
       'data[User][pass]' : 'somepassword'
      }
    }, function(error, response, body) {

      var sessionCookie = response.headers['set-cookie'][response.headers['set-cookie'].length - 1];
      sessionCookie = sessionCookie.split(';');
      sessionCookie = sessionCookie[0];

      // trying with this way  the same results

      /*
         res.set({'Cookie' : sessionCookie}).
           redirect('http://somesite.somedomain.com/Users');
      */

      // and same results too in this one

      /*
         res.setHeader('Cookie', sessionCookie).
           redirect('http://somesite.somedomain.com/Users');
      */


      res.header('Cookie', sessionCookie)
         .redirect('http://somesite.somedomain.com/Users');

      //the output shows the cookie is saved just fine on headers

      /*console.log(res.header());*/


      // Doing this!!! -> WORKS!! , but i need be on the browser not here.

      /*
       request.get('http://somesite.somedomain.com/Users', {
          headers: {"Cookie" : sessionCookie},
        }, function (error, response, body) {
          if(response.statusCode == 200) {
           console.log(body);
          }
       });
      */

    });
});

Uncomment the last commented piece of code (from request.get), i can see this works nice, and is printing the HTML (body) of the logged in website users home page, but when i try to set the Cookies in headers and redirect to the site in order to make login on the browser, this doesn't work

Is possible do this by this way?

I'm missing something?

Thanks in advance.

Was it helpful?

Solution

@Gonzalo do you still have access to the (server/code) running the cakephp app?

you would need to add a cross-origin header to allow your node.js (express) app to issue POST requests to the cakephp app/website.

e.g:

<?php
 header("Access-Control-Allow-Origin: *");

or specific to CakePHP:

$this->response->header('Access-Control-Allow-Origin', '*');

then you can test it by running:

curl -H "Content-Type: application/json" -dv "{'data[User][email]':'someuser@somedomain.com','data[User][pass]' : 'somepassword'}" http://somesite.somedomain.com/Users/login

Confirm that the headers contain an entry similar to:

Access-Control-Allow-Origin: *

This will allow you to access the PHP app from both Node (using request) and in your client app.

More info:

Let me know (in the comments) if you are still unable to send POST requests to the CakePHP app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top