How to use cookies from response in redirect request

Viewed 202

enter image description here

I am running a node.js app locally, in which I am trying to programmatically login into a server.

As shown in the figure, the login process consists of two steps. Firstly, when a user hits the /login route, receiving an authentication cookie in return. And secondly, a redirect to the / route using this cookie, or at least this is how I understand it.

This works fine in browsers, and in Postman, but it fails using the node.js app, for which I tried most of the popular http libraries (axios, follow-redirects, and built-in https).

The reason which I think it fails, is that the app does both requests together, without sending the auth cookie in the GET / request.

So my question is, how to force the app to use to returned cookie in the redirected request?

Below is a snippet of one of my trials using Axios:

axios({
    method: 'post',
    url: 'www.example.com/login',
    withCredentials: true,
    crossdomain: true,
    data: loginCreds,
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Cache-Control": "no-cache",
        "Content-Length": Buffer.byteLength(loginCreds),
        "Host": 'www.example.com'
    }
}).then(res => {
      console.log(res);
    });
}).catch(err => {
    console.log(err);
});
0 Answers
Related