3

I have some code that connects to a MySQL db to auth any user... this code works fine. The problem is when I receive an empty array from MySQL, it crashes nodejs...

This is part of the code in everyauth... sql is a exported function...

var promise = this.Promise();

sql.getUserInfo(userInfo, function(user, err) {
    if(err) return promise.fulfill([ err ]);

    promise.fulfill(user);
});

return promise;

I have no idea what promise is, but don't think that's the problem. This is the error in console when nodejs crashes:

starting step - extractLoginPassword
...finished step
starting step - authenticate
[]//--- it cames from mysql when there is no user and password 

timers.js:96
            if (!process.listeners('uncaughtException').length) throw e;

This is the exported function that calls the db and extracts information:

exports.getUserInfo = function(user, callback) {
  var email = user.email;
  var passwd = user.password;
  var sql = "SELECT id_email, contrasena FROM usuarios.usuario WHERE id_email = ? AND contrasena = ? ";
  var params = [email, passwd];

  client.query(sql, params, function(err, rows) {

    if (err) return callback(null, err);
    if (rows && rows.length > 0) {
      var user = rows[0];

      callback(user,err);
    }
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
andrescabana86
  • 1,778
  • 8
  • 30
  • 56
  • 1
    everyauth seems rather exception prone; if any part of the auth times out, your app gets an exception at the top level. I moved to [Passport](http://passportjs.org/) and have been rather happy since. – Asherah Jun 04 '12 at 01:11
  • i try passport and have this error: failed to serialize user into session when i try to get same param – andrescabana86 Jun 04 '12 at 01:22
  • 1
    Make sure you register serialization functions with Passport using `passport.serializeUser` and `passport.deserializeUser`. Details at the bottom of this page: http://passportjs.org/guide/configuration.html – Jared Hanson Jun 04 '12 at 02:33
  • thank you... it works with passport but i think it is not the answer to my proyect... thx anyway! – andrescabana86 Jun 04 '12 at 04:02

1 Answers1

0

Just check if query actually returns something, and fulfill some error if it is

sql.getUserInfo(userInfo, function(user, err) {
                if(err) return promise.fulfill([ err ]);
                if(!user) return promise.fulfill(['Authentication error']);
                promise.fulfill(user);
            });

should work fine ^

Masquer
  • 280
  • 1
  • 3
  • 9