REST Web Service in Node.js
•
routes/user.js:
router.post('/login', function(req, res) {
let sql1 = `SELECT * FROM users WHERE email = ? AND password = ?`;
let values = [ req.body.email, req.body.password];
dbcon.query(sql1, values, function(err, user, fields) {
if (err) throw err;
if (user.length > 0) {
var loginhash = crypto.randomBytes(24).toString('hex');
let sql2 = `UPDATE users SET hash = ? WHERE id = ?`;
let values2 = [loginhash, user[0].id];
dbcon.query(sql2, values2, function(err2, data2, fields2) {
if (err2) throw err2;
res.json({status: 200, id: user[0].id, name: user[0].name,
hash: loginhash, totalcoins: user[0].totalcoins,
totaldeaths: user[0].totaldeaths});
}
);
}
else {
res.json({status: 200, id: -1, name: "", hash: "", totalcoins: -1,
totalcoins: -1});
}
}
)
}
);