2

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.

6
  • You say " the line 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... ?
    – MarcoS
    Commented May 5, 2016 at 8:55
  • @MarcoS u right i treated the var token as a variable.. how to i check in the console if token is created? because even the next lines, response.json are not fetching??
    – Kob_24
    Commented May 5, 2016 at 9:00
  • Is console.log(token) statement not even executed? If it is, what exactly does it print?
    – MarcoS
    Commented May 5, 2016 at 9:02
  • nothing is coming out to the console
    – Kob_24
    Commented May 5, 2016 at 9:02
  • So it is not executed. You should try to debug the code flow, then... For example adding a console.log('calling jwt.sign()') statement just before the call to jwt.sign(), and so on...
    – MarcoS
    Commented May 5, 2016 at 9:04

1 Answer 1

1

Which jwt node module are you using?
I suppose it is not node-jsonwebtoken, since it does not support expiresInMinutes, but only expiresInMinutes...

However, you should enclose the jwt.sign() call in a try/catch block, to catch any error it throws...:

try {
  var token = jwt.sign(
    {
      username: data[0].username,
    },
    superSecret,
    {
      expiresIn: 1440 * 60,
    }
  );
} catch(err) {
  console.error('error signing json web token:', err);
}
6
  • var jwt = require('jsonwebtoken') this one, jsonwebtoken
    – Kob_24
    Commented May 5, 2016 at 9:11
  • See my updated answer: try using a try/catch block, and use expiresIn instead of expiresInMinutes...
    – MarcoS
    Commented May 5, 2016 at 9:14
  • Effectively, I don't see any superSecret definition, in your controller... :-(
    – MarcoS
    Commented May 5, 2016 at 9:28
  • just a dump question :$ 1440 * 60 its going to expire in 24h?
    – Kob_24
    Commented May 5, 2016 at 9:34
  • Yes, I did only convert 1440 minutes to seconds... You could also use "1 day", or the like, since expiresIn is rauchg/ms.js format (see github.com/rauchg/ms.js)...
    – MarcoS
    Commented May 5, 2016 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.