2014-08-29 100 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}}