I am trying to validate an API key to run an API endpoint function.
server.js
let db = require("./sql/rest_functions") // Custom modules
let session = require("./sql/session_management")
app.get("/node/api/mssql/test", function(req, res) {
if(session.validateKey(req)) {
db.mssqlQuery(req, res)
}
})
session.js
exports.validateKey = function(req) {
return sql.connect(config.properties)
.then(pool => {
return pool.request()
.query("SELECT CASE WHEN EXISTS (SELECT * FROM Login WHERE (username = '" + req.header("username") + "' AND apiKey = '" + req.header("apiKey") + "')) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END")
.then(response => {
console.log(typeof response[0]['']) // returns boolean
return response[0][''] // return nested value to get true / false response
})
})
}
The validateKey
function is returning true and false (I think as plain text), and I would like it to resolve to a boolean that can be passed into the API test function.
I have tried this JSON.parse
method and the session.validateKey(req) == 'true'
design pattern, but neither case resolves to an expected behavior.
How can I resolve the returned value to verify the user's credentials?