title: NodeJS sessions, cookies and mysql
body: <p>I'm trying to build an auth system and I have app.js</p>
  
  <pre><code>var express = require('express')
    , MemoryStore = require('express').session.MemoryStore
    , app = express();
  
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'keyboard cat', store: new MemoryStore({ reapInterval: 60000 * 10 })}));
  app.use(app.router);
  </code></pre>
  
  <p>and the route.index as</p>
  
  <pre><code>var express = require('express')
    , mysql = require('mysql')
    , crypto = require('crypto')
    , app = module.exports = express();
  
  app.get('/*',function(req,res){
      var url = req.url.split('/');
      if (url[1] == 'favicon.ico')
          return;
  
      if (!req.session.user) {
          if (url.length == 4 &amp;&amp; url[1] == 'login') {     
              var connection = mysql.createConnection({
                  host     : 'localhost',
                  user     : 'user',
                  password : 'pass',
              });
              var result = null;
              connection.connect();
              connection.query('use database');
              var word = url[3];
              var password = crypto.createHash('md5').update(word).digest("hex");
              connection.query('SELECT id,level FROM users WHERE email = "'+url[2]+'" AND password = "'+password+'"', function(err, rows, fields) {
                if (err) throw err;
                  for (i in rows) {
                      result = rows[i].level;
                  }
                  req.session.user = result;
              });
              connection.end();
          }
      }
  
  console.log(req.session.user)
  </code></pre>
  
  <p>when I access <a href="http://mydomain.com/login/user/pass" rel="nofollow">http://mydomain.com/login/user/pass</a> a first time it shows in the last console call  but a second time access the cookie is clean</p>