Documentation for “ensureAuthentication” “isAuthenticated” passport's functions?

前端 未结 2 2064
-上瘾入骨i
-上瘾入骨i 2020-12-07 12:41

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

相关标签:
2条回答
  • 2020-12-07 13:06

    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 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
    });
    
    0 讨论(0)
  • 2020-12-07 13:21

    the reason it return false is mostly because its declared below the route definition. i am doing it in other file so i use it like this

    //auth check
    function auth(req,res,next){
        if(req.isAuthenticated()){
            next();
        } 
        else{
            res.redirect("/fail");}
    }
    
    //routes
    require("./routes/myroute")(app,auth);
    
    0 讨论(0)
提交回复
热议问题