[英]ERR_HTTP_INVALID_STATUS_CODE — server goes off when logging on the website fails
I want to do authentication on my website.我想在我的网站上进行身份验证。 I did it with passport.js, and everything seems to be good, but there is one big issue.
我用passport.js做的,一切似乎都很好,但是有一个大问题。 If i type wrong username or password while logging in, the whole server goes down.I have got ' if ' statements in my code which should show flash massages when password is wrong.
如果我在登录时输入错误的用户名或密码,整个服务器就会关闭。我的代码中有“if”语句,当密码错误时应该显示 flash 消息。 It works perfectly with registration.
它与注册完美配合。 Below are parts of code in my files with login authentication and whole error from the console.
以下是我的文件中的部分代码,其中包含登录身份验证和来自控制台的整个错误。
error code错误代码
https://prnt.sc/s70s5m https://prnt.sc/s70s5m
file routes.js文件路由.js
const User = require("../models/user");
const Post = require("../models/post");
module.exports = function(app,passport){
app.get("/login", function(req,res){
res.render("login", {message:req.flash("loginMessage")});
});
app.post("/login", passport.authenticate("local-login", {
successRedirect:"/",
failureRedorect:"/login",
failureFlash:true
}))};
file passport.js文件护照.js
passport.use('local-login', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
User.findOne({ 'local.username' : username }, function(err, user) {
if (err)
return done(err);
if (!user)
{
return done(null, false, req.flash('loginMessage', 'No user found.'));
}
if (!user.validPassword(password))
{
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
}
else
return done(null, user);
});
}));
file user.js文件 user.js
const mongoose = require("mongoose"),
bcrypt = require("bcrypt-nodejs");
var userSchema = new mongoose.Schema({
local: {
username:String,
password:String
}
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.local.password);
};
It's typed wrong in failureRedorect:"/login"
.在
failureRedorect:"/login"
中输入错误。 Correct is: failureRedirect.正确的是:failureRedirect。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.