I've encountered an issue with the authentication process when transitioning from the deprecated 'request' module to the 'node-fetch' module. While the authentication request functions as intended with 'request', it fails to return the necessary 'authentication token' cookie with 'node-fetch'.
Below is the code that successfully retrieves the cookies using 'request'
// Working code with 'request'
var callback1 = function(err, httpResponse, body){
console.log("Correctly prints all the cookies we want: ");
console.log(httpResponse.headers["set-cookie"]);
if (err){console.log("here it is!"); throw err;}
else {
//do more with response
}
};
var callback0 = function(err, httpResponse0, body){
console.log("Check that sent cookies are identical, from request:");
console.log(httpResponse0.headers["set-cookie"][0].substr(0,httpResponse0.headers["set-cookie"][0].indexOf(";")));
if (err){throw err;}
else {
// Additional logic here
}
};
var options0 = {
// Request options for 'request'
};
request(options0, callback0);
And here is the code with 'node-fetch' that fails to return the auth_token cookie correctly:
// Code with 'node-fetch'
const fetchOptions0 = {
// Fetch options for 'node-fetch'
};
fetch(urlString0, fetchOptions0)
.then(res0 => {
console.log("Check that sent cookies are identical, from fetch:");
console.log(res0.headers.raw()['set-cookie'][0].substr(0, res0.headers.raw()['set-cookie'][0].indexOf(";")));
const FormData = require('form-data');
const myForm = new FormData();
myForm.append('email', myEmail);
myForm.append('password', myPassword);
var fetchOptions1 = {
// Fetch options for POST request
};
fetch(urlString1, fetchOptions1)
.then(res1=> {console.log("Incorrect cookie, missing auth:"); console.log(res1.headers.raw()['set-cookie']);});
});
I have attempted to use JSON.stringify for the form data based on suggestions from this answer, but it did not resolve the issue.