“ensureAuthentication"文档“已认证"护照的作用? [英] Documentation for "ensureAuthentication" "isAuthenticated" passport's functions?

查看:18
本文介绍了“ensureAuthentication"文档“已认证"护照的作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一段时间,但找不到明确的文档来源.当我搜索这些时,第一个 Google 搜索结果是 StackOverflow.

I've been looking for a while, and can't see to find a definitive documentation source. When I search for these, the first Google results are to StackOverflow.

还有类似的中间件功能吗?

Are there any more middleware functions similar to this?

推荐答案

虽然没有在任何容易找到的地方明确记录,但您可以看到 isAuthenticatedisUn​​authenticated 标志的位置在 https://github.com/jaredhanson/的 Passport 代码中设置护照/blob/a892b9dc54dce34b7170ad5d73d8ccfba87f4fcf/lib/passport/http/request.js#L74.

While not explicitly documented anywhere easily found, you can see where the the isAuthenticated and isUnauthenticated flags are set in the Passport code at https://github.com/jaredhanson/passport/blob/a892b9dc54dce34b7170ad5d73d8ccfba87f4fcf/lib/passport/http/request.js#L74.

ensureAuthenticated 不是官方的,但可以通过以下方式实现:

ensureAuthenticated is not official, but can be implemented via the following:

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
});

这篇关于“ensureAuthentication"文档“已认证"护照的作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆