I'm trying to login to my Node.js (Express) app with another Node.js created script to try create a virtual bot on my app.
First, i sent a POST via "request" package to my /api/user/login and then i tried to get data from /api/user/session where i must be logged.
Request script:
request.post({
url: "http://localhost:1717/api/user/login",
form: {
email: "xxx@xxx",
password: "xxx"
}
}, (err1, res1, body1) => {
console.log(res1.statusCode); // console returned 200 cause login was successfull
request.get({
url: "http://localhost:1717/api/user/session",
}, (err2, res2, body2) => {
console.log(res2.statusCode); // console returned 401 cause im not logged
});
});
Express script:
app.post("/api/user/login", (req, res) => {
const email = req.body.email;
const password = sha256(req.body.password);
if(isValid(email) && isValid(password)) // for example
req.session.userID = 1; // for example
res.sendStatus(200);
}
else{
res.sendStatus(404);
}
});
app.get("/api/user/session", (req, res) => {
const userID = req.session.userID;
if(userID){
res.status(200).send(req.session.id);
}
else{
res.sendStatus(401) // Unauthorized
}
});
Probably i need to send a valid cookie to GET method on Express. Please let me know how to do this. Thanks.