title: req.isAuthenticated() returning false all the time in passportjs
body: <p>I have passport setup in express and req.isAuthenticated() always returning due to wrong order of code in index.js. I am not able to figure out the order in which I have to write code , I searched for other answers in stackoverflow and I couldnt figure out the right order since my code has different dependencies please help . This is my index.js file :</p>
  
  <pre><code>const winston = require("winston");
  const express = require("express");
  const config = require("config");
  const app = express();
  const passport = require('passport');
  const flash    = require('connect-flash');
  var session = require('express-session');
  const cookieParser = require('cookie-parser');
  app.use(express.json());
  app.use(cookieParser());
  
  
  
  
  
  
  
  
  app.use(session({ 
    secret: 'ilovescotchscotchyscotchscotch',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true }
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  
  require('./services/passport')(passport);
  
  require("./startup/db")();
  
  
  app.use(flash());
  
  
  
  require("./startup/logging")();
  require("./startup/cors")(app);
  require("./startup/routes")(app);
  
  require("./startup/config")();
  //require("./startup/validation")();
  
  
  
  const port = process.env.PORT || config.get("port");
  const server = app.listen(port, () =&gt;
    winston.info(`Listening on port ${port}...`)
  );
  
  module.exports = server;
  </code></pre>
  
  <p>Thanks.</p>