【问题标题】:Intercepting flash message截取 Flash 消息
【发布时间】:2016-12-15 00:33:19
【问题描述】:

我正在为我的应用程序使用带有“本地”策略的 Passport 身份验证。我希望用户能够知道用户输入的用户名和密码是否无效,如果有效则重定向到仪表板。 这就是我验证用户的方式:

router.post('/login', passport.authenticate('local', {failureRedirect: '/login', failureFlash: 'Invalid username or password.'}), function(req, res, next){
    res.redirect('/users/dashboard');
});

重定向部分工作正常,但用户输入无效用户/通过failureFlash: 'Invalid username or password.' 中的 Flash 消息显示在登录页面上。 这就是我处理登录路径的方式:

router.get('/login', function(req, res, next) {
  res.render('pages/login', {'title' : 'VoteCenter - Login', message: req.flash('message')});
});

我需要在req.flash('??????') 中使用什么来拦截来自failureFlash 的flash 消息?

【问题讨论】:

标签: node.js express passport.js passport-local connect-flash


【解决方案1】:

您可以通过req.flash('error')(默认)获取消息

您也可以使用自定义消息名称传递 failureFlash 对象:

failureFlash: { type: 'authError', message: 'Invalid username or password.'}

然后得到消息req.flash('authError')

【讨论】:

  • 这正是我想要的。为我工作。
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多