node.js - How to access Passport user in main.js and how to add middleware?
问题描述
I want to access my user which is logged in. My serialize and deserialize user is valid. I see every info like part of a console.log(req).
Part of console.log(req) is include my user information but i can't access it with req.user it returns undefined.
This is my (req) log:
user: User {
dataValues: {
id: 1,
UserName: 'testuser',
UserPw: '1234567',
ResetPasswordToken: '3c7560f9c888227a64a90e271d2b58a0630f24c6dde46458d2981058d007120e',
ResetPasswordTokenExpire: 2020-12-20T15:49:51.000Z,
createdAt: 2020-12-20T15:49:51.000Z,
updatedAt: 2021-01-12T15:24:20.000Z
},
_previousDataValues: {
id: 1,
UserName: 'testuser',
UserPw: '1234567',
ResetPasswordToken: '3c7560f9c888227a64a90e271d2b58a0630f24c6dde46458d2981058d007120e',
ResetPasswordTokenExpire: 2020-12-20T15:49:51.000Z,
createdAt: 2020-12-20T15:49:51.000Z,
updatedAt: 2021-01-12T15:24:20.000Z
},
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
route: Route {
path: '/success',
stack: [ [Layer] ],
methods: { get: true }
},
[Symbol(kCapture)]: false,
[Symbol(RequestTimeout)]: undefined
}
And i want to access with my view engine(handlebars)
I think i access req.user it would be : req.locals.user = req.user
解决方案
如果您使用 Sequelize.js,则必须为 LocalStrategy 和反序列化部分返回 user.dataValues。
推荐阅读
- html - 如何使用管理面板将新帖子上传到 Github 托管的网页?
- java - 为什么 BitmapFactory.decodeStream 返回 null?
- swiftui - SwiftUI - 导航时视图是否保持活跃?
- css - 我的网络应用程序的菜单显示多余的边距
- django - moduleNotFoundError:没有名为“booktime.main”的模块
- lua - 如何添加多个变量
- php - 隐藏 woocommerce 订单页面了解更多按钮
- types - 在调用 strcpy() 时确定第一个参数的类型:(char *) vs char[]
- python - 如何在 Python 中部分拆分并获取字符串的第一部分?
- c++ - Felgo live 自定义模块未安装错误