我正在使用 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/