2012-04-22 51 views
0

我想在出现错误凭据的情况下为用户显示错误消息。我怎么能在Express JS中做到这一点?我尝试了下面的代码,但它不起作用:在ExpressJS响应中发送JQuery Mobile错误消息

app.get('/', function(req, res, next) { 
     passport.authenticate('local', function(err, user, info) { 
     if (err) { return next(err) } 
     if (!user) { return res.send($("<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'><h1>YOUR MESSAGE</h1></div>") 
     .css({ "display": "block", "opacity": 0.96, "top": $(window).scrollTop() + 100 }) 
     .appendTo($.mobile.pageContainer) 
     .delay(800) 
     .fadeOut(400, function() { 
     $(this).remove(); 
     }) 
     ); } 
     req.logIn(user, function(err) { 
     if (err) { return next(err); } 
     return res.redirect('/account'); 
     }); 
     })(req, res, next); 
    }); 

这是什么问题?

谢谢,

+0

这是什么呢? – Mustafa 2012-04-22 20:51:15

+0

身份验证。查看并导航到自定义回调部分http://passportjs.org/guide/authenticate.html – 2012-04-23 03:10:38

+0

不,我的意思是这段代码做了什么,有什么不起作用? – Mustafa 2012-04-23 07:45:32

回答

0

错误消息是来自策略的验证回调?像这样:

// verify callback 
return done(null, false, { message: 'Invalid password' }); 

如果是这样的话,它会风作为info参数上面的代码片段:

passport.authenticate('local', function(err, user, info) { 
    // arguments to this callback are what was given to done() 
    // info.message => 'Invalid password' 

    var msg = "<h1>" + info.message + "<h1>" 
    res.send(msg) 
}