title: React App with Express Backend & express-session
body: <p>I currently have a React App (via <code>create-react-app</code>) and an Express server for my API calls. They are both running separately and have been working fine until I ran into this problem: </p>
  
  <p>I'm also using <code>express-session</code> and <code>passport</code> for user authentication. I'm able to authenticate users but the session doesn't persist between API calls. If I successfully login, the next time I make an API call, <code>req.sessionID</code> will be different, and <code>req.isAuthenticated()</code> will return <code>false</code>.</p>
  
  <p>Here is my server code: </p>
  
  <pre><code>'use strict'
  
  var express         = require('express');
  var path                = require('path');
  var bodyParser      = require('body-parser');
  var cookieParser    = require('cookie-parser');
  var mongoose      = require('mongoose');
  var passport      = require('passport');
  var session       = require('express-session');
  var cors          = require('cors');
  var flash         = require('connect-flash');
  
  var store         = require('./store');
  var database      = require('./database');
  
  var app     = express();
  var port    = process.env.port || 3001;
  var router  = express.Router();
  
  var promise = mongoose.connect('mongodb://localhost:27017/lifeless-db', {useMongoClient: true});
  
  //app.use(cors());
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  app.use(cookieParser('lifeless-secret-01890'));
  app.use(session({
    secret: 'lifeless-secret-01890',
    saveUninitialized: true,
    cookie: {
      secure: false,
    }
  }));
  app.use(flash());
  
  app.use(passport.initialize());
  app.use(passport.session());
  // Initialize Passport
  var authinit = require('./auth/init');
  authinit(passport);
  
  // For debugging:
  app.use(function(req, res, next) {
    console.log("SessionID: " + req.sessionID);
    console.log(req.isAuthenticated() ? "This user is logged in" : "This user is NOT logged in");
    next();
  });
  
  
  
  // GET
  
  app.get('/items', function(req, res) {
    store.getAllItems((items) =&gt; {
      if(!items) return res.send({error: true, message: 'Error loading store :/', items: null});
      else return res.send({error: false, message: 'Success', items: items});
    });
  });
  
  app.get('/login', function(req, res) {
    console.log(req.flash());
    console.log("Login fail");
    res.send({error: true, message: 'Unknown error'});
  });
  
  
  // POST
  
  app.post('/message', function(req, res) {
    database.submitMessage(req.body.email, req.body.message, (success) =&gt; {
      if (success) return res.send({error: false, message: 'Success'});
      else return res.send({error: true, message: 'Error sending message'});
    });
  });
  
  
  app.post('/login', passport.authenticate('local', {failureRedirect: '/login', failureFlash: true}), function(req, res) {
    console.log(req.flash());
    console.log("Login success");
    return res.send({error: false, message: 'Success'});
  });
  
  
  
  // SERVER
  
  app.listen(port, function(){
    console.log('Server started.');
    console.log('Listening on port ' + port);
  });
  </code></pre>
  
  <p>And here is an example of an API call from the React App (using <code>axios</code>):</p>
  
  <pre><code>login(e) {
      e.preventDefault();
  
      axios({
        method: 'post',
        url: 'http://localhost:3001/login',
        data: {
          username: this.state.username,
          password: this.state.password,
        }
      })
      .then((response) =&gt; {
        console.log(response.data);
        if (response.data.error) {
          this.setState({error: true, errmessage: response.data.message});
        } else {
          this.setState({redirect: true});
        }
      })
      .catch((error) =&gt; {
        this.setState({error: true, errmessage: 'Error logging in'});
      });
    }
  </code></pre>
  
  <p>I figure there must be some way to have React store the session somehow (?) but I'm fairly new to React and don't really know how to use it with an express backend. </p>