I have a problem to make working my token validation function in nodeJS. My "return" always returns "undefined" ... But the console.log works. This function's utility is to validate a JWT Token, comparing token information with my database for the id and admin tag :
const verifyTokenValidity = (token) => {
try{
const decodedToken = jwt.decode(token, 'JWT_PASS_SECRET')
const checkTokenIdentity_query = sql = fs.readFileSync('./sqlquery/checkTokenIdentity_query.sql').toString()
const tok_account_id = decodedToken.account_id
const tok_is_admin = decodedToken.isadmin
pool.query(checkTokenIdentity_query, [tok_account_id], (error, results) => {
const bdd_account_id = String(JSON.stringify(results.rows[0].ac_account_id)).replace(/['"]+/g, '')
const bdd_is_admin = String(JSON.stringify(results.rows[0].ac_isadmin)).replace(/['"]+/g, '')
if (tok_account_id == bdd_account_id || tok_is_admin == bdd_is_admin){
return 'valid'
}
else {
return 'invalid'
}
})
}
catch(error) {
return 'invalid'
}
}
When I execute the function with a valid token :
console.log(verifyTokenValidity('ayJhbG.........iaXNhZG1pbiI6ImZhbHNlIiwi......TkzLCJleHAiOjE2MTYxODgzOTN9......JT0PhXCjV00'))
>>>>> undefined
If I replace all the occurences of return by console.log I have a "valid" result. I think there is a callback problem but after a lot of research and test I don't understand what is wrong with my code.
Thanx for helping me,
F4nch.