【问题标题】:Documentation for "ensureAuthentication" "isAuthenticated" passport's functions?“ensureAuthentication”“isAuthenticated”护照功能的文档?
【发布时间】:2012-12-20 18:17:41
【问题描述】:

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

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

【问题讨论】:

    标签: express passport.js


    【解决方案1】:

    它返回 false 的原因主要是因为它在路由定义下面声明。 我在其他文件中这样做,所以我像这样使用它

    //auth check
    function auth(req,res,next){
        if(req.isAuthenticated()){
            next();
        } 
        else{
            res.redirect("/fail");}
    }
    
    //routes
    require("./routes/myroute")(app,auth);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 2020-11-08
      • 2020-10-25
      • 2019-01-02
      • 1970-01-01
      • 2016-06-03
      相关资源
      最近更新 更多