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

This error is currently occurring on my development server. This happens when logging in:

Is this passport a specific issue? Please help, this works very well on a production server

I have this in my app.js application

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 it is in auth mode

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 problem:

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) returns below

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

Passport never seems to have been added

+4
source share
3 answers

The fact that the same code works in one environment but not another makes me believe in an environment or configuration problem.

, , ? , - .

, .

+1

, "node_modules" . "npm i" ( sudo, ).

0

Try enabling auth passports in the main code itself rather than exporting it. This isolates the path problem. If this gives the same error, try reinstalling the passport data.

0
source

Source: https://habr.com/ru/post/1674252/


All Articles