title: NodeJS with express-sessions and express-mysql-session isn't setting the sessions. Where did I screw up?
body: <p>Trying to get sessions working using Node.js and Express for my personal project. I'm using a MySQL database as my session store, and have installed the following modules:</p>
  
  <ul>
  <li><strong>express</strong>    </li>
  <li><strong>body-parser</strong>, to get POST data</li>
  <li><strong>mysql</strong>, to connect to my DB     </li>
  <li><strong>cors</strong>  </li>
  <li><strong>express-session</strong></li>
  <li><strong>express-mysql-session</strong></li>
  <li><strong>bcrypt</strong>, to compare POSTed data with DB hashes</li>
  </ul>
  
  <p>I'm sending a POST request containting login info through javascript from a page, which I compare with an hash from the database. If it matches, I create the session for the user. I want to prevent the user from logging in again if it has a session, but it looks like the session cookie isn't being stored. I verified this by looking at <code>req.session</code>, but the <code>user</code> object I created never appears there.</p>
  
  <p>Records in the database are being created: if I login with correct data, a new record is created. I'm not sure if this is supposed to happen but if I login again with the same user it creates a new record.</p>
  
  <p>Here's what I've got:</p>
  
  <pre><code>var express    = require('express');
  var app        = express();
  var bodyParser = require('body-parser');
  var mysql      = require('mysql');
  var cors       = require('cors');
  var session    = require('express-session');
  var MySQLStore = require('express-mysql-session')(session);
  var bcrypt     = require('bcrypt');
  
  var options = { ... };
  var pool = mysql.createPool(options);
  var sessionConnection = mysql.createConnection(options);
  
  var sessionStore = new MySQLStore({
      expiration: 10800000,
      createDatabaseTable: true,
      schema: {
          tableName: 'USERS_SESSIONS',
          columnNames: {
              session_id: 'session_id',
              expires: 'expires',
              data: 'data'
          }
      }
  }, sessionConnection);
  
  // i'll change key &amp; secret later
  app.use(session({
      key: '69Atu22GZTSyDGW4sf4mMJdJ42436gAs',
      secret: '3dCE84rey8R8pHKrVRedgyEjhrqGT5Hz',
      store: sessionStore,
      resave: false,
      saveUninitialized: true
  }));
  
  app.use(cors());
  
  ...
  
  var router = express.Router();
  
  router.post('/login', function(req, res){
      if (req.session.user){
           console.log('already logged in');
      }else{
          // get connection from pool, retrieve user record
          // PLAIN is password from request, row.HASH is the record's hash
          bcrypt.compare(PLAIN, row.HASH, function(err, match){
              // do error handling
              // when match = true do this
  
              req.session.user = {
                  id: row.ID,
                  nickname: row.NICK,
                  isAuthed: true
              };
              res.sendStatus(200); // else send 401
              return;
          });
      }
  });
  </code></pre>
  
  <p>After successfully logging in, I check my session like this:</p>
  
  <pre><code>router.get('/session', function(req, res){
      res.json(req.session);
  });
  </code></pre>
  
  <p>And I get the following:</p>
  
  <pre><code>{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
  </code></pre>
  
  <p>As you can see there's no <code>user</code> object. I'm not getting any errors and I can't figure out where's the problem. </p>