node.js - PassportJS 重命名 "user"对象

标签 node.js mongodb authentication passport.js passport-local

我正在使用 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();
    }
}

我想改用以下内容(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 source code

关于node.js - PassportJS 重命名 "user"对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577662/

相关文章:

node.js - 在 Laravel 项目中安装 NodeJS、Gulp 和 Elixir

node.js - Node 失败退出代码 1

node.js - 在node.js中需要 ('dotenv' ).config()

json - JSON文档数据库中的键成本(mongodb,elasticsearch)

WebApi 上的 AngularJS 和 OWIN 身份验证

rest - 检查 Microsoft 图形 token 是否过期

node.js - Nodejs子进程: write to stdin from an already initialised process

scala - 部署使用 IDEA 构建的 Scala 应用程序

mongodb - Pymongo 查找其字段是给定字符串的子字符串的文档

javascript - 有没有办法使用 Web 身份验证 API (WebAuthn) 获取 U2F 设备的稳定唯一标识符?