0

everybody

I am working on an application which is using IBM db2 as database and using passport js for authentication purpose.

so my question is how do i compare the values which are coming from login form to that which are stored in database.

passport.use(new LocalStrategy({
      usernameField: 'euser',
      passwordField: 'epass'
},
function(username, password, done){
      process.nextTick(function() {
         /* how do i open connection to db2 and compare username and password 
         with the data stored in database? */
      });  
}
));

how do i open connection to db2 and compare values in order to authenticate user and start session.

user2881430
  • 367
  • 1
  • 5
  • 17

1 Answers1

0

Taken from the passport.js docs; for example sake, change the usernameField to username, passwordField to password. You could probaly switch the User object with the variable assignments for login results with database results if you so desire.

var username = ibm d2 query results for username;
var password = ibm d2 query results for password;


     var User = {//login form username/password
username: '', //whatever they entered will go here,
password: '' 
};

.... // the above is passed as arguments to the function(username, password, done) {

User is the object returned from your login form. findOne is a passportJs function that compare the value of your username property to the database query results you assigned to variable username. If match is success and not err, the results are passed as the user object to the next function.

function(username, password, done) {
    User.findOne({ username: username }, function(err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' });
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
      }
      return done(null, user);
    });
  }
));

function returns user object results if no errors are present and match was found. Note, this is error first style so he returns a null value for a successful result as opposed to just the result. null in this case meaning no errors.

deek
  • 1,085
  • 1
  • 9
  • 27