2014-08-29 102 views
4

我使用快递和把手的节点。我有一个登录表单,并且应该向用户显示登录错误消息。我的代码如下: 验证(使用护照):快递,把手显示Flash消息

... 
else if (password != user.password) { 
      return done(null, false, req.flash('message', 'Wrong password')); 
... 

在路线我得到这个:

app.post('/sign-in', passport.authenticate('local', { 
     successRedirect : '/', // redirect to the home page 
     failureRedirect : '/sign-in', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

然后渲染我的车把模板,

app.get('/sign-in', function(req, res) { 
     res.render("signin.handlebars", {layout: 'users.handlebars', action: 'Sign in', message: req.flash('message'), 
        csrf: 'CSRF token goes here' }); 
    }) 

问题是,当输入错误的密码时,闪光消息不会按需显示。

编辑:我的快递设置是:

app.engine('handlebars', handlebars.engine); 
app.set('view engine', 'handlebars'); 
app.set('models', __dirname + '/models'); 
app.use(express.static(__dirname + '/public'));  // set the static files location /public/img will be /img for users 
app.use(cookieParser()); 
app.use(expressSession({secret:'somesecrettokenhere', resave: true, 
         saveUninitialized: true, })); 
app.use(passport.initialize()); 
app.use(passport.session()); 
//app.use(session({ store: new RedisStore })); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: true 
})); 
app.use(flash()); 
app.use(morgan("dev")); 
app.disable('x-powered-by'); 
app.use(function(err, req, res, next) { 
    res.status(err.status || 500); 
}); 
+0

你有没有安装快车闪光? – Vinz243 2014-08-29 09:47:09

+0

我正在使用连接闪光灯 – Denny 2014-08-29 09:56:57

+0

您是否尝试了其他路线中的简单闪光灯消息? – Vinz243 2014-08-29 10:00:56

回答

4

我解决它顺便说一句,像这样: ...

if (!user) { 
      return done(null, false, { 
       message: 'The email you entered is incorrect' 
      }); 

... 在JSON编码的消息。 然后在路线我:

app.get('/sign-in', function(req, res) { 
     res.render("signin.handlebars", {layout: 'users.handlebars', action: 'Sign in', ***error: req.flash('error')***, 
        csrf: 'CSRF token goes here' }); 
    }) 
在我的车把模板

然后:

{{#if error}} 
    <div class="alert alert-danger">{{error}}</div> 
{{/if}}