Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 319
Jarosław Rewers Avatar asked Dec 07 '25 06:12

Jarosław Rewers


1 Answers

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.

like image 179
robertklep Avatar answered Dec 09 '25 19:12

robertklep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!