gpt4 book ai didi

javascript - Nodejs Passport 失败消息不起作用

转载 作者:行者123 更新时间:2023-12-01 17:22:00 25 4
gpt4 key购买 nike

我的应用程序上运行有 Passport Local。

成功会带来验证,而失败则不会。然而,我试图通过失败消息和转义操作来使其更好地失败。

尝试按照 Passport 文件进行操作。

由于某种原因没有出现错误消息,希望得到一些帮助!

这是本地策略

// Configure the local strategy for use by Passport.
passport.use(new Strategy(
function(username, password, cb) {
db.users.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false,req.flash('message','Invalid username or password')); }
if (user.password != password) { return cb(null, false,req.flash('message','Invalid username or password')); }
return cb(null, user);
});
}));

身份验证

//Home
app.post('/home.ejs',
passport.authenticate('local'),
function(req, res) {
res.redirect('/');
});

如果这更准确,我还尝试进行身份验证,但也没有运气

  //Home
app.post('/home.ejs', function( req, res, next) { passport.authenticate('local', function(err, user, info) {

if (err) { return next(err); req.flash('message','Invalid username or password') }

if (!user) { return res.redirect('/'); failureFlash: 'Invalid username or password' }

req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/');

});
})(req, res, next);
});*/

最佳答案

应该是

passport.use(new LocalStrategy ...

而不是你的

passport.use(new Strategy(.....

关于javascript - Nodejs Passport 失败消息不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61276545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com