Classified
Classified

Reputation: 222

Passportjs this.set is not a function when calling setPassword

I am having trouble with passportJS setPassword. This code was working in a previous project (passport-local-mongoose: 4.0.0, passport: 0.3.2) however, now with newer versions I am facing problems (passport-local-mongoose: 5.0.1, passport: 0.4.0.

I am receiving this.set is not a function when calling passportjs setPassword()

const {promisify} = require('es6-promisify');

exports.resetPassword = async (req, res) => {
  const user = await User.findOne({
    resetPasswordToken: req.params.token,
    resetPasswordExpires: { $gt: Date.now() }
  });
  
  if (!user) {
    req.flash('error', 'Password reset is invalid or has expired');
    return res.redirect('/login');
  }

  const setPassword = promisify(user.setPassword, user);
  await setPassword(req.body.password);

  user.resetPasswordToken = undefined;
  user.resetPasswordExpires = undefined;
  const updatedUser = await user.save();
  await req.login(updatedUser);
  req.flash('success', 'Your password has been reset!');
  res.redirect('/login');
}

The error.

TypeError: this.set is not a function
    at Promise.resolve.then.then.then.then.then.salt (...\node_modules\passport-local-mongoose\index.js:98:14)
From previous event:
    at exports.updatePassword (...auth.controller.js:146:9)
    at 
    at process._tickCallback (internal/process/next_tick.js:188:7)

Upvotes: 1

Views: 144

Answers (1)

user11401130
user11401130

Reputation:

Just in case someone else goes through the same problem,

Starting with version 5.0.0 passport-local-mongoose is async/await enabled by returning Promises for all instance and static methods except serializeUser and deserializeUser.

const user = new DefaultUser({username: 'user'});
await user.setPassword('password');
await user.save();
const { user } = await DefaultUser.authenticate()('user', 'password');

Upvotes: 1

Related Questions