PassportJS 重命名“用户”对象

And*_*ito 4 authentication mongodb node.js passport-local passport.js

我正在使用 PassportJS 进行登录功能有没有办法更改请求中的“用户”对象名称?

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

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

我想改用以下内容(req.candidate):

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

Tom*_*máš 6

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

 passport.initialize( {
      userProperty: 'student' // defaults to 'user' if omitted
     })
Run Code Online (Sandbox Code Playgroud)

此代码集:req.student

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

看源码: Passport源码