title: Passport deserializeUser() is never called with Axios http request
body: <p>When I send a request via axios to my Express app, req.isAuthenticated() is always false and req.user does not exist, even after logging in. But when I send a request via Postman to the app, it works. It seems that deserializeUser() is never called so the req.session.passport field is never populated.</p>
  
  <p>I've tried all of the suggestions online, any help is appreciated.</p>
  
  <p>External request:</p>
  
  <pre><code>async tweet(content) {
      try {
        await axios.post(this.url + '/tweets/new', {
          content: content,
          withCredentials: true,
          headers: {
            'Content-Type': 'application/json',
          }
        });
  
      } catch (err) {
        console.log(err);
      }
  }
  </code></pre>
  
  <p>index.js</p>
  
  <pre><code>const passport = require('passport');
  const cookieParser = require('cookie-parser');
  const session = require('express-session');
  const RedisStore = require('connect-redis')(session)
  const redisCookie = require('heroku-redis-client');
  
  require('./config/passport')(passport);
  
  // required for passport
  app.use(cookieParser());
  
  app.use(session({
    // secret: process.env.SECRET || 'enteryoursecrethere',
    secret: 'enteryoursecrethere',
    cookie: { maxAge: 3600000 },
    resave: true,
    store: new RedisStore({client: redisCookie.createClient()}),
    saveUninitialized: true
  }));
  
  app.use(passport.initialize());
  app.use(passport.session());
  
  app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Origin', req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
    next();
  });
  </code></pre>
  
  <p>router.js</p>
  
  <pre><code>var tweets = require('../controllers/tweets');
  var router = express.Router();
  var isLoggedIn = require('../middleware/isLoggedIn');
  
  router.post('/tweets/new', isLoggedIn, tweets.tweet);
  </code></pre>
  
  <p>middleware/isLoggedIn.js</p>
  
  <pre><code>module.exports = (req, res, next) =&gt; {
      // If user is authenticated in the session, carry on.
      if (req.isAuthenticated()) {
        next();
        return
      }
  
      // If they aren't redirect them to the home page.
      res.redirect('/');
  }
  </code></pre>
  
  <p>passport.js</p>
  
  <pre><code>const LocalStrategy = require('passport-local').Strategy;
  const User = require('../models').User;
  const Sequelize = require('sequelize');
  
  module.exports = function(passport) {
    // The login request establishes a session maintained in a browser cookie.
    // Requests after the login request not contain credentials,
    // but rather the unique cookie that identifies the session. The user object
    // is constructed to and from the ID in the cookie.
  
    // Converts user to user id.
    passport.serializeUser(function(user, done) {
      done(null, user.id);
    });
  
    // Converts user id to user, stored in req.user.
    passport.deserializeUser(function(id, done) {
      User.findById(id).then(function(user) {
        done(null, user);
      }).catch(function(err) {
        done(err);
      });
    });
  
    /* ============Login============ */
    passport.use('local-login', new LocalStrategy({
      usernameField: 'username',
      passwordField: 'password',
      passReqToCallback : true // Send entire request for flash message.
    }, loginCallback));
  
    passport.use('local-signup', new LocalStrategy({
      usernameField: 'username',
      passwordField: 'password',
      passReqToCallback : true
    }, signupCallback));
  
  };
  
  function loginCallback(req, username, password, done) {
    if (req.isAuthenticated()) {
      return done(null, req.user);
    }
    // Look up the user by username.
    User.findOne({
      where: {
        username: username
      }
    }).then(function(user) {
      if (!user) {
        return done(null, false, req.flash('loginUsernameMessage', 'Wrong username.'));
      }
  
      if (!user.validatePassword(password)) {
        return done(null, false, req.flash('loginPasswordMessage', 'Wrong password.'));
      }
  
      return done(null, user.get());
    }).catch(function(err) {
      return done(err);
    });
  }
  
  function signupCallback(req, username, password, done) {
    // Asynchronous. User.findOne wont fire unless data is sent back.
    process.nextTick(function() {
      if (password != req.body.password_confirm) {
        return done(null, false, req.flash('signupMessage', 'Passwords don\'t match.'));
      }
  
      // Find a user whose email is the same as the forms email.
      // We are checking to see if the user trying to login already exists.
      User.findOne({
        where: {
          [Sequelize.Op.or]: [ { username: username }, { email: req.body.email }]
        }
      }).then(function(user) {
        // Check to see if theres already a user with that username or email.
        if (user) {
          return done(null, false, req.flash('signupMessage', 'That email or username is already taken.'));
        }
        // Create the user.
        var data = {
          fname: req.body.fname,
          lname: req.body.lname,
          username: username,
          email: req.body.email,
          password: User.generateHash(password)
        }
  
        User.create(data).then(function(newUser) {
          return done(null, newUser);
        }).catch(function(err) {
          return done(err);
        });
      }).catch(function(err) {
        return done(err);
      });
    });
  }
  </code></pre>