首页 > 解决方案 > 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

标签: node.jsexpresssessionhandlebars.jspassport.js

解决方案


如果您使用 Sequelize.js,则必须为 LocalStrategy 和反序列化部分返回 user.dataValues。


推荐阅读