F4LLCON F4LLCON - 1 year ago 70
Node.js Question

node route passport current user id

the code I'm using gets current user id from cookies, but the user logged in does have another user id. I'm using passport authentication for the user. I want

to be the current passport user

current code uses cookie user id:


app.get('/portal', isLoggedIn, function (req, res) {
var user_id = req.cookies ?
req.cookies.user_id : undefined;

user_id: user_id
exec(function (err, boxes) {
if (err) return next(err);

res.render('portal', {
user: req.user,
boxes: boxes

I've tried changing

var user_id = req.cookies ?
req.cookies.user_id : undefined;

with e.g.
var user_id = req.user.user_id;
, and some other syntaxis but non work. What am I doing wrong?

Answer Source

Are you using sessions ? ..If yes try req.session.user._id

    secret: 'MySuperSecret',
    saveUninitialized: true,
    resave: false,
    duration: 30 * 60 * 1000,
    activeDuration: 5 * 60 * 1000

And when u log in with user store user obj in req.session, so you can access it anywhere you like.

req.session.user = user;


Other solution that works when using passport: change var user_id = req.user.id; to var user_id = req.user.user_id;