【发布时间】:2012-12-20 18:17:41
【问题描述】:
我一直在寻找一段时间,但找不到确定的文档来源。当我搜索这些时,Google 的第一个结果是 StackOverflow。
还有类似的中间件功能吗?
【问题讨论】:
标签: express passport.js
我一直在寻找一段时间,但找不到确定的文档来源。当我搜索这些时,Google 的第一个结果是 StackOverflow。
还有类似的中间件功能吗?
【问题讨论】:
标签: express passport.js
它返回 false 的原因主要是因为它在路由定义下面声明。 我在其他文件中这样做,所以我像这样使用它
//auth check
function auth(req,res,next){
if(req.isAuthenticated()){
next();
}
else{
res.redirect("/fail");}
}
//routes
require("./routes/myroute")(app,auth);
【讨论】:
虽然没有在任何容易找到的地方明确记录,但您可以在 https://github.com/jaredhanson/passport/blob/a892b9dc54dce34b7170ad5d73d8ccfba87f4fcf/lib/passport/http/request.js#L74 的 Passport 代码中看到 isAuthenticated 和 isUnauthenticated 标志的设置位置。
ensureAuthenticated不是官方的,但可以通过以下方式实现:
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
});
【讨论】:
if (req.isAuthenticated()),第 9 行应为 ..., ensureAuthenticated, ... 查看以下内容以获得更好的示例。 github.com/jaredhanson/passport-local/blob/master/examples/…