JavaScript Passport.js verification always fails

My passport middleware is not working. When I call passport.authenticate() on a route to confirm the user is authenticated, I am redirected to the failure page. The login route however works as expected, and I am redirected successfully to the correct page, where the failure middleware redirect is called and I am sent back to the login page.

I have a Passport strategy like so:

module.exports = function(){ 
  var passport = require('passport');
  var LocalStrategy = require('passport-local').Strategy
  var User = require('../models/user');
  // used to serialize the user for the session
  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  // used to deserialize the user
  passport.deserializeUser(function(id, done) {
    User.getUserById(id, function(err, user) {
      done(err, user);
    });
  });

  passport.use(new LocalStrategy({
    usernameField: "email",
    passwordField: "password"
  }, function(email, password, done){
    User.getUserByEmail(email, function(err, user){
      if(err) throw err;
      if(!user){
        console.log('unknown user');
        return done(null, false, {message: 'Email not recognised, please try again'});  
      } 
      User.comparePassword(password, user.password, function(err, isMatch){
        if(err) throw err;
        if(isMatch){
          return done(null, user);
        } else {
          console.log('Invalid password');
          return done(null, false, { message: 'Password not recognised, please try again' });
        }
      });
    });
  }));
}; 

Logging in is fine using:

router.post('/user/login', 
  passport.authenticate('local', { 
    successRedirect: '/clients',
    failureRedirect: '/user/login', 
    failureFlash: true
  }
));

My '/clients' route is like so, where the authentication fails and redirects incorrectly back to the login page:

router.get('/clients', 
  passport.authenticate("local", { 
    failureRedirect: "/user/login",
    failureFlash: "not verified" 
  }), function(req, res, next) {
  res.locals.client = null;
  Client.find({})
    .select("name")
    .select("_id") 
    .exec(function(err, clients){
      res.render('client/views/clients', { 
        title: 'Clients',
        clients: clients 
      });
  });  
});

Server passport initialization like so:

//passport
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
require("./user/services/passport")(passport);

What am I doing wrong?

Answer:1

In node I am accustomed to handling errors with callbacks. I'm trying to make the jump to async... await, which uses a method of error handling that I am not used to. I am wondering, when throw is ...

In node I am accustomed to handling errors with callbacks. I'm trying to make the jump to async... await, which uses a method of error handling that I am not used to. I am wondering, when throw is ...

I am getting an error in IE11 when attempting to call a function that is executing on click in a radio button. Here is the html for the radio button: <input type="radio" name="filter" value="...

I am getting an error in IE11 when attempting to call a function that is executing on click in a radio button. Here is the html for the radio button: <input type="radio" name="filter" value="...

  1. javascript object doesn't support property or method
  2. javascript object doesn't support this action
  3. javascript object doesn't support includes
  4. javascript object doesn't support foreach
  5. javascript object doesn't support indexof

I made a progressive web application with firebase hosting. When I do some changes and I deploy I don't see the changes when I visit the app from browser. This is my code in sw.js: self....

I made a progressive web application with firebase hosting. When I do some changes and I deploy I don't see the changes when I visit the app from browser. This is my code in sw.js: self....

  1. update data with firebase
  2. update firebase
  3. update firebase cli
  4. update firebase tools
  5. update firebase database android
  6. update firebase database
  7. update firebase android
  8. update firebase realtime database
  9. update firebase functions
  10. update firebase npm
  11. update firebase javascript
  12. update firebase android studio
  13. update firebase realtime database android
  14. update firebase angular
  15. update firebase document
  16. update firebase version
  17. update firebase data android
  18. update firebase react native
  19. update firebase database javascript
  20. update firebase admin

I have this array of buttons in my React application: const buttons = [ <Button label='Close' onClick={props.handleClose} />, <Button label='Save' onClick={props.handleSubmit} />, <...

I have this array of buttons in my React application: const buttons = [ <Button label='Close' onClick={props.handleClose} />, <Button label='Save' onClick={props.handleSubmit} />, <...

  1. react rendering object object