我尝试使用passport.js进行Node.js ajax身份验证,我想在var ModuleA = (function () {
var ModuleA = function () {
console.log('ModuleA');
};
ModuleA.prototype = (function () {
var moduleA = function () {
};
return {
moduleA: moduleA
}
}());
return ModuleA;
})();
new ModuleA();
页面中显示消息。我应该在我的护照策略中使用/login
,然后ajax呼叫成功结束将向其页面显示成功数据。但我无法猜测我如何使用res。在战略中。请参阅以下代码,
login.ejs
res.send
app.js
<div id="messages"></div>
<!-- and there is a form, when form submitted, the ajax call executed.-->
<!-- ...ajax method : POST, url : /login, data: {}, success:... -->
<!-- If ajax call success, get 'result' data and display it here -->
我在谷歌搜索了一些文档,人们通常使用&#39; flash()&#39;在连接闪存模块,但我认为这个模块需要重新加载页面,这不是我想要的,所以请帮助我,让我知道是否有更好的方法。谢谢。
答案 0 :(得分:2)
您可以使用自定义回调代替直接插入Passport中间件,以便将req, res, next
对象传递给Passport函数。
您可以在路由处理程序/控制器中执行类似操作(这可以直接从Passport文档中获取):
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});