Passport.js - passing {user: req.user} to template implicitly?

前端 未结 1 1782
一向
一向 2020-12-30 10:53

Using Passport.js and Express for multiple projects now, I have noticed myself doing this over and over again, namely specifying { user: req.user } explicitly f

相关标签:
1条回答
  • 2020-12-30 11:42

    You could use a simple middleware for that:

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

    This will make a user variable available in all templates, provided that req.user is populated. Make sure that you declare that middleware after you declare the passport.session middleware, but before any routes.

    0 讨论(0)
提交回复
热议问题