且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“ensureAuthentication"文档“已认证"护照的作用?

更新时间:2023-12-03 18:55:22

虽然没有在任何容易找到的地方明确记录,但您可以看到 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
});