function findByUsernamePassword(username, password, callback) {
  User.findOne({ username }, function(err, user) {
    if (err) throw err;

    // if user with given username not found
    if (!user) {
      callback(null, { success: false, message: `User with username ${username} not found`});
    }
    else {
      // Compare password and respond
      user.comparePassword(password, (err, isMatch) => {
        if (!isMatch) {
          callback(null, {
            success: false,
            message: 'Authentication failed. Wrong password.'
          });
        }
        else {
          var auth_token = jwt.sign({
            username: user.username
          }, serverConfig.secret, { expiresIn: '1440h' });

          callback(user, {
            success: true,
            message: 'Authentication success.',
            username: user.username,
            auth_token
          });
        }
      });
    }
  });
}
/**
 * This function finds given 'username' from database and compares against given
 * password, then calls the callback function which indicates successful or unsuccessful
 *
 * @param {string} username - The username to check the password for
 * @param {string} password - The password for username
 * @param {function} callback - The function to call after user is authenticated or authentication failed
 */
This function finds given 'username' from database and compares against given
password, then calls the callback function which indicates successful or unsuccessful

@param {string} username - The username to check the password for
@param {string} password - The password for username
@param {function} callback - The function to call after user is authenticated or authentication failed
This function finds given ' username ' from database and compares against given password then calls the callback function which indicates successful or unsuccessful @param { string } username - The username to check the password for @param { string } password - The password for username @param { function } callback - The function to call after user is authenticated or authentication failed
function findByUsernamePassword(username, password, callback) {
  User.findOne({ username }, function(err, user) {
    if (err) throw err;
    if (!user) {
      callback(null, { success: false, message: `User with username ${username} not found`});
    }
    else {
      user.comparePassword(password, (err, isMatch) => {
        if (!isMatch) {
          callback(null, {
            success: false,
            message: 'Authentication failed. Wrong password.'
          });
        }
        else {
          var auth_token = jwt.sign({
            username: user.username
          }, serverConfig.secret, { expiresIn: '1440h' });
          callback(user, {
            success: true,
            message: 'Authentication success.',
            username: user.username,
            auth_token
          });
        }
      });
    }
  });
}
function findByUsernamePassword ( username , password , callback ) { User . findOne ( { username } , function ( err , user ) { if ( err ) throw err ; if ( ! user ) { callback ( null , { success : false , message : ` ${ username } ` } ) ; } else { user . comparePassword ( password , ( err , isMatch ) => { if ( ! isMatch ) { callback ( null , { success : false , message : 'Authentication failed. Wrong password.' } ) ; } else { var auth_token = jwt . sign ( { username : user . username } , serverConfig . secret , { expiresIn : '1440h' } ) ; callback ( user , { success : true , message : 'Authentication success.' , username : user . username , auth_token } ) ; } } ) ; } } ) ; }
This function finds given 'username' from database and compares against given
 password, then calls the callback function which indicates successful or unsuccessful
This function finds given ' username ' from database and compares against given password then calls the callback function which indicates successful or unsuccessful
// if user with given username not found // Compare password and respond