title: Express session reset for every request
body: <p>I am trying to use session variables in express to manage user sign in. I can't get the variable to work over different requests. Here is my code:</p>
  
  <pre><code>var config = require('config'),
  
      ejs = require('ejs'),
  
      util = require('util'),
  
      express = require('express'),
      bodyParser = require('body-parser'),
      cors = require('cors'),
      moment = require('moment'),
      plaid = require('plaid'),
      mariadb = require('mariadb'),
  
      fs = require('fs'),
      http = require('http'),
      https = require('https'),
  
      session = require('express-session'),
      cookieParser = require('cookie-parser');
  
  ...
  
  var app = express();
  
  app.use(cookieParser());
  app.use(
      session({
          resave: false,
          saveUninitialized: false,
          secret: 'debug',
          cookie: { secure: true }
      })
  );
  
  ...
  
  app.post('/tokensignin', function (req, res) {
      async function verify() {
          const ticket = await googleAuthClient.verifyIdToken({
              idToken: req.body.idtoken,
              audience: GOOGLE_AUTH_CLIENT_ID,
          });
          const payload = ticket.getPayload();
          const userid = payload['sub'];
          if (payload['aud'] == GOOGLE_AUTH_CLIENT_ID) {
              pool.getConnection().then(conn =&gt; {
                  conn.query("CALL getUserByGoogleId(?)", [userid])
                      .then(rows =&gt; {
                          res.sendStatus(200);
                          if (rows[0]) {
                              req.session.test = "Hello, world!";
                              console.log("req.session.test = " + req.session.test);
                              req.session.user = {
                                  firstName: rows[0].firstName,
                                  lastName: rows[0].lastName,
                                  avatar: rows[0].imageUrl,
                              };
                          } else {
                              conn
                                  .query("CALL createUser(?,?,?,?,?)) VALUES (?,?,?,?,?)"
                                      , [userid, req.body.firstName, req.body.lastName, req.body.imageUrl, req.body.email]
                                  )
                                  .then(rows =&gt; {
                                      req.session.user = {
                                          firstName: rows[0].firstName,
                                          lastName: rows[0].lastName,
                                          avatar: rows[0].imageUrl,
                                      };
                                  })
                                  .catch(err =&gt; {
                                      console.log("error: " + err);
                                      res.send(err);
                                  });
                          }
                      })
                      .catch(err =&gt; {
                          res.send(err);
                      });
                  conn.end();
              });
          }
      }
      verify().catch(console.error);
  });
  </code></pre>
  
  <p>The variables are properly stored the first time, but when I redirect to the home page, <code>req.session.test</code> and <code>req.session.use</code> are undefined.</p>
  
  <p>Here's the code for that:</p>
  
  <pre><code>app.all('/home', function (req, res) {
      console.log("req.session.test = " + req.session.test);
      console.log(req.session);
      return res.render('home.ejs', {
          PLAID_PUBLIC_KEY: PLAID_PUBLIC_KEY,
          PLAID_ENV: PLAID_ENV,
          PLAID_PRODUCTS: PLAID_PRODUCTS,
          APP_MODE: config.APP_MODE,
          APP_PORT: config.APP_PORT,
          URL: config.URL,
          test: req.session.test,
          user: req.session.user,
      });
  });
  </code></pre>
  
  <p>I'm using:</p>
  
  <ul>
  <li>Express 4.16</li>
  <li>express-session 1.15.6</li>
  <li>cookie-parser 1.4.3.</li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p>My website is using SSL, so my <code>cookie</code> should have <code>secure:true</code>.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I tried moving <code>res.sendStatus(200)</code> so that the method now looks like this:</p>
  
  <pre><code>conn.query("CALL getUserByGoogleId(?)", [userid])
      .then(rows =&gt; {
          if (rows[0]) {
              req.session.test = "Hello, world!";
              console.log("req.session.test = " + req.session.test);
              req.session.user = {
                  firstName: rows[0].firstName,
                  lastName: rows[0].lastName,
                  avatar: rows[0].imageUrl,
              };
          } else {
              conn.query("CALL createUser(?,?,?,?,?)) VALUES (?,?,?,?,?)"
                  , [userid, req.body.firstName, req.body.lastName, req.body.imageUrl, req.body.email])
              .then(rows =&gt; {
                  req.session.user = {
                      firstName: rows[0].firstName,
                      lastName: rows[0].lastName,
                      avatar: rows[0].imageUrl,
                  };
              })
              .catch(err =&gt; {
                  console.log("error: " + err);
                  res.send(err);
              });
          }
          res.sendStatus(200);
          conn.end();
      })
      .catch(err =&gt; {
          res.send(err);
      });
  });
  </code></pre>
  
  <p>and got the same result.</p>