How to know if user is logged in with passport.js?

后端 未结 6 1031
日久生厌
日久生厌 2020-12-02 05:01

I\'ve been reading passport.js info and samples for two days, but I\'m not sure after that I did all the process of authenticating.

How do I know if I\

6条回答
  •  天涯浪人
    2020-12-02 05:27

    If you would like to use it in your templates as your code sample seems to indicate you can create some middleware such as this:

    app.use(function (req, res, next) {
      res.locals.login = req.isAuthenticated();
      next();
    });
    

    Place that code somewhere after you have setup passport.

    And then use it in your template (swig example)

    {% if login %}
    
    {% else %} 
    
    {% endif %}
    

提交回复
热议问题