gpt4 book ai didi

node.js - 无法显示来自 Passport.js 的 Flash 消息

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:12 25 4
gpt4 key购买 nike

如果身份验证失败,我在发送 Flash 时遇到问题。除了一切都好之外。当我将示例 ( https://github.com/jaredhanson/connect-flash ) 复制到我的应用程序时,闪现消息工作正常...

这是我的代码:

//routes
app.get('/login', function (req,res){
res.render('login', {
authMessage: req.flash('authMessage')
});
});

app.post('/login', passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: true
}), function(req,res){
res.send('ok');
});

//local strategy
passport.use(new LocalStrategy(
function(username, password, authCheckDone) {
UserDetails.findOne({username : username}).then(
function(user){
if (!user) {
console.log('bad username');
return authCheckDone(null, false, {authMessage: 'Wrong name'});
}
if (user.password !== password) {
console.log('bad password');
return authCheckDone(null, false, {authMessage: 'Wrong password'});
}
return authCheckDone(null, user);
}),
function(error){
return authCheckDone(error);
};
}
));

//ejs
<% if (authMessage.length > 0) { %>
<p>test</p>
<%= authMessage %>
<% } %>

我在某处读过这个问题可能与安全性有关并且不使用 https,但如果是这样,来自 github 的示例也不起作用......

最佳答案

如果我没记错的话,您传递给 LocalStrategy 的回调应该将请求对象作为第一个参数。所以你的函数看起来像这样:

function(req, username, password, authCheckDone) {
// your code

之后,根据我的示例,您应该按以下方式返回 authCheckDone 的结果:

return authCheckDone(null, false, req.flash('authMessage', "your message"));

但请确保通过以下方式创建 LocalStrategy 对象:

new LocalStrategy({
passReqToCallback: true // don't forget this
}, function(req, username, password, authCheckDone) {

关于node.js - 无法显示来自 Passport.js 的 Flash 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34708692/

25 4 0
文章推荐: c# - 访问数据表中的下一行
文章推荐: c# - 在没有 base64 编码的情况下通过 https 发送字节数组的方法?
文章推荐: angularjs - Angular2 应用程序可以通过 package.json 安装和运行吗?
文章推荐: c# - List 对象引用