护照-本地猫鼬中缺少密码错误

时间:2020-09-12 14:48:26

标签: node.js express mongoose passport.js

我正在从Coursera学习Node js课程,同时在学习使用通行证的用户身份验证时,我遇到了这个错误。我完全按照课程中所说的去做,但是我遇到了这个错误!

我了解该课程的某些内容已被弃用,但是如何解决此错误?

错误

Connected correctly to server
(node:7856) UnhandledPromiseRejectionWarning: MissingPasswordError: No password was given
    at B:\Node\Node JS\conFusionServer\node_modules\passport-local-mongoose\index.js:103:17
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:7856) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)        
(node:7856) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

我的模特

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

var User = new Schema({
    // username: { // auto added by passport
    //     type: String,
    //     required: true,
    //     unique: true,
    // },
    // password: {
    //     type: String,
    //     required: true,
    // },
    admin: {
        type: Boolean,
        default: false,
    }
});

User.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', User);

我的身份验证

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user');

exports.local = passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

我的路线

router.post('/signup', (req, res, next) => {
  // User.findOne({ username: req.body.username })
  User.register(new User({ username: req.body.username }, req.body.password, (err, user) => {
    if (err) {
      console.log(err);
      res.statusCode = 500;
      res.setHeader('Content-Type', 'application/json');
      res.json({ err: err });
    } else {
      console.log(user);
      passport.authenticate('local')(req, res, () => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        res.json({ success: true, status: 'Registration Successful' });
      });
    }
  })); // passport-local-mongoose
});

我的app.js

app.use(passport.initialize());
app.use(passport.session());

app.use('/', indexRouter);
app.use('/users', usersRouter);

function auth(req, res, next) {
  if (!req.user) { // auto loaded by passport
    var err = new Error('You are not authenticated!!!');
    err.status = 401;
    return next(err);
  } else {
    next();
  }
}

app.use(auth);

我的邮递员 enter image description here

1 个答案:

答案 0 :(得分:0)

解决了! 这是Router中的愚蠢错字!我没有关闭用户对象,然后将密码作为参数传递,而是忘记了关闭用户对象!