例如,在Meteor中,有类似的东西 Router.plugin('ensureSignedIn');Router.plugin('ensureSignedIn', { except: ['home', 'atSignIn', 'atSignUp', 'atForgotPassword']}); 因此,未签名的用户无法访问除上述四个以外的其他路由
Router.plugin('ensureSignedIn');
Router.plugin('ensureSignedIn', {
except: ['home', 'atSignIn', 'atSignUp', 'atForgotPassword']
});
因此,未签名的用户无法访问除上述四个以外的其他路由.
如何在express.js中执行此操作?我也在使用passport.js.
我不熟悉Meteor,但您可以执行以下操作,假设您希望仅向经过身份验证的用户(护照)提供页面.function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated())
return next();
else
// Return error content: res.jsonp(...) or redirect: res.redirect('/login')
}
app.get('/account', ensureAuthenticated, function(req, res) {
// Do something with user via req.user
});
ensureAuthenticated函数只是一个例子,您可以定义自己的函数.调用next()会继续请求链.
