passport-local-mongoose 中缺少密码错误
Missing password error in passport-local-mongoose
我正在从 coursera 学习 node js 课程,在学习使用 passport 进行用户身份验证时,我遇到了这个错误。我完全按照课程中的内容进行了操作,但出现此错误!
我了解该课程的某些内容已被弃用,但如何解决此错误?
错误
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);
我的邮递员
已解决!
这是路由器中的一个愚蠢的错字!我没有关闭用户对象然后将密码作为参数传递,而是忘记关闭用户对象!
我正在从 coursera 学习 node js 课程,在学习使用 passport 进行用户身份验证时,我遇到了这个错误。我完全按照课程中的内容进行了操作,但出现此错误!
我了解该课程的某些内容已被弃用,但如何解决此错误?
错误
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);
我的邮递员
已解决! 这是路由器中的一个愚蠢的错字!我没有关闭用户对象然后将密码作为参数传递,而是忘记关闭用户对象!