I have the following setup for an express app. I am trying to check if a token provided in the route is legitimate using a helper function, then if it is decide which res to send to the requester. No matter what though, Invalid Token: -1 is returned whether or not a legitimate token is provided.
var pool = sql.createPool({
connectionLimit: 50,
host : "localhost",
port : 3306,
user : "bla",
password : "bla",
database : "bla",
multipleStatements: true
});
...
function authorize(token) {
var check = "select * from User where Token=?";
var status = -1;
pool.getConnection(function (err, connection) {
connection.query(check, [token], function (err, rows) {
if (err) {
status = err;
};
status = rows.length;
});
connection.release();
});
return status;
}
app.get('/user/token/:Token', function(req, res) {
var token = req.params.Token;
var date = currentDate();
var endpoint = req.route;
var stmt = "insert into APIAccess (Token, Endpoint, Date) values (?, ?, ?);select * from User where Token=?";
var auth = authorize(token);
pool.getConnection(function (err, connection) {
if (auth == 1) {
connection.query(stmt, [token, endpoint, date, token], function (err, rows) {
if (err) throw err;
res.send(JSON.stringify(rows[1]));
});
connection.release();
} else {
res.send("Invalid token: " + auth);
}
});
});
What is going on here? I know the two statements in the multipleStatements set worked fine before I tried implementing this authorization bit, and that they aren't even being reached here, and that this code is syntactically correct...