Learner here and I got stuck with an error when learning and building a basic login route in JS using passport. Error is MongooseError: Model.findOne() no longer accepts a callback. I'm getting this within this block of code in the .findOne() method.
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) { //error handler catches it here
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(password)) { return done(null, false); }
return done(null, user);
});
}
));
I tried .then().catch() instead of using a callback in the .findOne() method itself and modified the code to following,
passport.use(new passportLocal(
function(username, password, done) {
User.findOne({ username: username })
.then(user =>{
if (!foundUser) { return done(null, false); }
if (!foundUser.verifyPassword(password)) { return done(null, false); }
return done(null, user);
})
.catch(e =>{
return done(e);
})
}));
This didn't work and still left scratching my head.
The login route code is as follows;
router.get('/login', (req, res)=>{
res.render('users/login')
});
router.post('/login', passport.authenticate('local', {failureFlash: true, failureRedirect: '/login'}), async(req, res) => {
req.flash('success', 'Welcome Back.');
res.redirect('/campgrounds')
});
Out of ideas with my still fairly limited experience. Had a look at a few previous posts, but, they aren't related to passport authentication. Any input is hugely appreciated.