Saturday, April 15, 2017

How to fix: Nodejs passport.header issue, req.session.passport undefined

Leave a Comment

I am currently having this error in my Development Server. This occurs on login attempts:

Is this passport specific issue. Please help, this works very well in production server

I have this in my app.js

app.use(cookieParser('12345678')); app.use(session({ secret: '12345678', resave: false, saveUninitialized: true }));  app.use(validator()); app.use(passport.initialize()); app.use(passport.session());  app.use(flash()); // flash - Initializing app.use(compression());  app.use('/app', express.static(path.join(__dirname, '/app'), { maxAge: 7 * 86400000 })); app.use('/uploads', express.static(path.join(__dirname, '/uploads'), { maxAge: 7 * 86400000 })); app.set('view engine', 'pug'); app.locals.pretty = false; app.set('views', './views'); app.use(function (req, res, next) {     res.header("Access-Control-Allow-Origin", "*");     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");     next(); }); 

Then this in auth method

module.exports = function (passport) {      passport.serializeUser(function (user, done) {         done(null, user);     });      passport.deserializeUser(function (user, done) {         done(null, { id: user.id });     });      passport.use('site-register', new LocalStrategy({         usernameField: 'email',         passwordField: 'pwd',         passReqToCallback: true // allows us to pass back the entire request to the callback     },      function (req, email, pwd, done) {             process.nextTick(function () {              if( condition is ok){                   return done(null, user, { message: 'Login Success' });             }else{               return done(null, false, { message: 'Invalid Login, Please try another method to login' });         }    ));      ..... } 

Then the issue:

embah@devsertwo:~/node/nodeapp$ node app Server turned on with development mode on port 3002 /home/embah/node/nodeapp/routes/auth.js:384                                     req.session.passport.header = authHeader;                                                                 ^  TypeError: Cannot set property 'header' of undefined     at /home/embah/node/nodeapp/routes/auth.js:384:65     at /home/embah/node/nodeapp/controller/adaptor/mongodb.js:31:9     at Query.<anonymous> (/home/embah/node/nodeapp/node_modules/mongoose/lib/quer y.js:2180:28)     at /home/embah/node/nodeapp/node_modules/kareem/index.js:177:19     at /home/embah/node/nodeapp/node_modules/kareem/index.js:109:16     at _combinedTickCallback (internal/process/next_tick.js:67:7)     at process._tickCallback (internal/process/next_tick.js:98:9) embah@devsertwo:~/node/nodeapp$ Write failed: Connection reset by peer 

console.log(req.session) return below

Session {   cookie:    { path: '/',      _expires: null,      originalMaxAge: null,      httpOnly: true } } 

Seems passport was never added

2 Answers

Answers 1

The fact that the same code works in one environment but not the other leads me to believe it's an environment or configuration issue.

Maybe there's an incompatibility between some package versions that vary on production vs development? Try updating all your dependencies and see if anything changes.

Sometimes, changing the order of middleware can help as well.

Answers 2

It means that your "node_modules" folder is not updated in development. Delete the entire folder and run "npm i" (or with sudo if needed) again.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment