I have a function that selects username from mysql database and return results to post request as following:
function userAuthentication(user){
var deferred = Q.defer();
pool.getConnection(function(err, connection) {
if (err) {
var dbError = new Error('No db connection');
console.log(dbError);
}
else {
var selectUserName = 'SELECT * FROM User WHERE username= '+connection.escape(user)+' ';
connection.query(selectUserName, function(error, rows){
if(error){
console.log(error);
deferred.reject(error);
}
else if(rows.length === 0){
var countError = new Error('No User Found');
console.log ("Database.js -> "+ countError);
deferred.reject(countError);
}else{
deferred.resolve(rows);
}
});
}
connection.release();
});
return deferred.promise;
}
the function userAuthentication
is called from the rout.post request
router.post('/checkAuthentication', function(request, response) {
database.userAuthentication(request.body.username).then(function(data) {
if(data[0].RowDataPacket!=0){
if(bcrypt.compareSync(request.body.password,data[0].password)){
var token = jwt.sign({
username: data[0].username,
}, superSecret,{
expiresInMinutes: 1440
});
console.log(token);
// return the information including token as JSON
response.json({
success: true,
message: 'Token Created',
token: token
});
}
else{
response.json({ success: false, message: 'Authentication failed. Wrong Password.' });
}
}else{
response.json({ success: false, message: 'Authentication failed. User not found.' });
}
});
});
i am trying to create a token when i check if inputed username and password matches the ones in mysqlDb, but unfortunately the token is not created; the line console.log(token)
is not printing any result into the console. what is the part i am missing??? I appreciate your help guys.
console.log(token)
is not printing any result into the console": what does taht mean, exactly? Is console.log statement not even executed? If it is, does it print an empty string, null, undefined... ?console.log(token)
statement not even executed? If it is, what exactly does it print?console.log('calling jwt.sign()')
statement just before the call tojwt.sign()
, and so on...