0

I'm trying to check the entered username and password stored in a database. My solution is not correct and I think there might be something better than my code.

Here it is thus far:

function login (username, password, callback) {
    var query = "SELECT  * FROM users WHERE username = ?";

    connection.query(query, [username], function (err, results) {
        if (err) return callback(err);
        if (results.length === 0) return callback();
        var user = results[0];

        if (!bcrypt.compareSync(password, user.password)) {
            return callback();
        }

        callback(null,   {
            id:          user.id.toString(),
        });

    });
}
app.get('/salam', function (req, res) {
    var username = 'mahdi';
    var originalPassword = 'a';

    login(username , originalPassword,function (callback) {
        console.log(callback);
    });
});

In my code, console.log(callback); returns null, but usernames and passwords are correct. How can I fix this?

1
  • You are already setting first parameter of callback as a NULL? callback(**null**, { id: user.id.toString(), }); Commented Aug 1, 2016 at 12:15

2 Answers 2

2

In your success callback function, you are having 2 arguments but in error callback, only one argument.

In error and success case, value of first parameter will always be null and in if (!bcrypt.compareSync(password, user.password)) { case, value of first argument will be undefined as there is no value being passed as argument.

Suggestion: Use first argument as Boolean(false or true) and based on the value, handle the callback.

function login(username, password, callback) {
  var query = "SELECT  * FROM users WHERE username = ?";

  connection.query(query, [username], function(err, results) {
    if (err) return callback(false);
    if (results.length === 0) return callback();
    var user = results[0];

    if (!bcrypt.compareSync(password, user.password)) {
      return callback(false);
    }
    callback(true, {
      id: user.id.toString(),
    });

  });
}
app.get('/salam', function(req, res) {
  var username = 'mahdi';
  var originalPassword = 'a';

  login(username, originalPassword, function(success, value) {
    if (success) {
      console.log(value);
    }
  });
});

Sign up to request clarification or add additional context in comments.

5 Comments

It won't always be null: there are two places in the OP's code (and one place in yours) where the function is called with no arguments, so then callback will be undefined.
@nnnnnn Updated.. Missed that part ;)
@Rayon Thanks, problem resolved sir :) wait to me for accept that
@mahdipishguy – I'm glad it helped mate! Happy Coding
@Rayon hi, could you help me on this topic? stackoverflow.com/q/38794164/6599627 Thanks
1

It should be, because you didn't pass anything in callback. Change like this :

function login (username, password, callback) {
    var query = "SELECT  * FROM users WHERE username = ?";

    connection.query(query, [username], function (err, results) {
        if (err) return callback(err);
        if (results.length === 0) return callback(null, false);
        var user = results[0];

        if (!bcrypt.compareSync(password, user.password)) {
            return callback(null, false);
        }

        callback(null,   true, {
            id:          user.id.toString(),
        });

    });
}
app.get('/check', function (req, res) {
    var username = 'mahdi';
    var originalPassword = 'a';

    login(username , originalPassword,function (err, result, id) {
        console.log(err);
        console.log(result);
        console.log(id);
    });
});

result is for finding out true|false of action. And id means when result is true

Also err for callback is needed to error handling

2 Comments

If you don't pass anything (which is the case in two places) the argument will be undefined, not null. But in what I assume it's the success case the OP's code explicitly passes null.
@Ebrahim Pasbani +1 Thanks Ebrahim agha

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.