Pat*_*ick 5 javascript middleware connect node.js express
Passport.js为node.js和Express提供了很好的身份验证,包括一个中间件解决方案:
ensureAuthenticated = function(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
return res.redirect("/login");
};
Run Code Online (Sandbox Code Playgroud)
如何在快速资源模块中使用此中间件?不幸,
app.resource('users', ensureAuthenticated, require('./resources/users'));
Run Code Online (Sandbox Code Playgroud)
不起作用.
小智 14
我知道这有点太晚了,原来的帖子得到了解答,但是,我正在寻找相同的答案并找到了我认为其他人可能想知道的解决方案.
只需确保从护照中调用ensureAuthenticated即可.
app.resource('users', passport.ensureAuthenticated, require('./resources/users'));
Run Code Online (Sandbox Code Playgroud)
它在这里找到:https://gist.github.com/1941301
解决方法.确保对所有请求进行身份验证,并忽略进入/ auth和/ auth/callback的请求.
app.all('*', function(req, res, next) {
if (/^\/auth/g.test(req.url)) {
return next();
} else if (req.isAuthenticated()) {
return next();
} else {
return next(new Error(401));
}
});
Run Code Online (Sandbox Code Playgroud)