Jarosław Rewers
Jarosław Rewers

Reputation: 1099

Cant access property of req.user

I'm writing in Node using passport.js to authenticate.

console.log(req.user);

returns

{ group: 'CuManager',
  password: 'password',
  username: 'administrator',
  cu_id: 2,
  _id: 569fd3f4328ef124be533caf }

but

console.log(req.user.cu_id);

returns

undefined

I was thinking that cu_id property maybe is not available due to serialization/deserialization, but it's there. I just can't access it. How can I do that? I need it for

find({cu_id : req.user.cu_id})

BTW I can't help thinking passport.js is little overcomplicated (maybe we just don't get along). What else can I use to authenticate users in Node (I use express)?

Upvotes: 3

Views: 859

Answers (2)

boomcode
boomcode

Reputation: 399

try parsing it in JSON.

JSON.parse(req.user)['cu_id']

Upvotes: 0

robertklep
robertklep

Reputation: 203534

If req.user is a Mongoose document, you should probably add cu_id to the proper schema.

Otherwise you can use something like req.user.toObject().cu_id to access the property, or make sure that somehow the result of doc.toObject() is assigned to req.user.

As for a Passport-replacement: this depends on how exactly you want to authenticate your users. If you're POST'ing a login form to an Express route, you can perform the password validation and session setup yourself.

Upvotes: 3

Related Questions