PassportJS重命名“用户”对象

我使用PassportJS作为我的loginfunction有没有办法改变请求中的“用户”对象名称?

这是现在它是如何工作的,默认情况下(req.user):

function (req, res, next) { if (!req.user) { req.flash('error', 'Please sign in to access this page.'); res.redirect('/login'); } else { next(); } } 

我想使用下面的代替(req.candidate):

 function (req, res, next) { if (!req.candidate) { req.flash('error', 'Please sign in to access this page.'); res.redirect('/login'); } else { next(); } } 

您可以在初始化中更改它:

  passport.initialize( { userProperty: 'student' // defaults to 'user' if omitted }) 

这个代码集: req.student

接下来你要做什么取决于你和使用的框架。

看源代码: Passport源代码