0

I am making a website (named webA) using nodejs. I want to be able to automatically log in another website (name webB) each time I access webA.

I searched at Google and realized that I need to use cookies to this? My code is now like below:

var cookie = require('cookie');
var escapeHtml = require('escape-html');
var http = require('http');
var url = require('url');

function onRequest(req, res) {

  var cookies = cookie.parse(req.headers.cookie || ''); 
    cookies.name = 'user_abcd';
    cookies.password = '1234';

    //post a request to webB ???
}

http.createServer(onRequest).listen(3000);

If I understand correctly, I guess I will need to 'construct' a request using the cookies I made, then send a post request to webB. Am I on the right track? Any codes will be highly appreciated.

wildcolor
  • 556
  • 1
  • 8
  • 22
  • This should done through proper SSO implementation. Spoofing auth cookies are not recommended. Moreover WebB needs to have proper `CORS` configuration to allow this. – nilobarp Sep 22 '17 at 03:40
  • @nilobarp thanks for quick reply. As a beginner in web scraping, I am just seeking for the most basic method to achieve automatically login function, which can significantly help me to understand the process, after which I can try to do it in more professional way. – wildcolor Sep 22 '17 at 03:48

1 Answers1

1

Cookies can be passed in the headers of a http request like so

var options = {
    hostname: 'example.de',
    path: '/pathexample',
    method: 'POST',
    headers: {
        'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
        'Cookie': '<cookie string>',
        'Accept': '/',
        'Connection': 'keep-alive'
    }
};

Then use this option object in

http.request(options, function (resp) { ... });

Also check HTTP CORS & Cookie Domains

nilobarp
  • 3,190
  • 2
  • 23
  • 32