【发布时间】:2018-07-08 13:08:30
【问题描述】:
我在 if 语句中有一个函数
isLoggedin() 有一个异步调用。
router.get('/', function(req, res, next) {
if(req.isLoggedin()){ <- never returns true
console.log('Authenticated!');
} else {
console.log('Unauthenticated');
}
});
如何在 if 语句中等待 isLoggedin()?
这是我使用护照的 isLoggedin 函数
app.use(function (req, res, next) {
req.isLoggedin = () => {
//passport-local
if(req.isAuthenticated()) return true;
//http-bearer
passport.authenticate('bearer-login',(err, user) => {
if (err) throw err;
if (!user) return false;
return true;
})(req, res);
};
next();
});
【问题讨论】:
标签: node.js express asynchronous ecmascript-6 passport.js