title: How to generate custom session id with express-session?
body: <p>I am using Redis as session store and I would also like to create custom session id with <code>express-session</code>. So far I achieved was creating session with constant string like this:</p>
  
  <p><code>redis configuration</code></p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var compression = require('compression');
  var session = require('express-session');
  var redis = require('redis');
  var RedisStore = require('connect-redis')(session);
  
  var routes = require('./routes/index');
  var api = require('./routes/api');
  
  var app = express();
  var client = redis.createClient();
  
  // compress all requests
  app.use(compression())
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');
  
  // uncomment after placing your favicon in /public
  //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(session({
    secret: 'SECRET',
    resave: false,
    saveUninitialized: false,
    store: new RedisStore({ host: 'localhost', port: 6379, client: client}),
    cookie: {
      maxAge: 60000
    },
    genid: function(req) {
      return "myApiKey";
    }
  }));
  app.use('/', routes);
  app.use('/request_api', api);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  </code></pre>
  
  <p>which creates keys in redis like below:</p>
  
  <pre><code>127.0.0.1:6379&gt; keys *
  1) "sess:myApiKey"
  </code></pre>
  
  <p>However, I could not generate unique id with <code>uuid</code> as mentioned in doc here <a href="https://github.com/expressjs/session#genid" rel="noreferrer">session-store github</a>. 
  I have tried inserting session value in my index routes file (<code>routes/index.js</code>) like this</p>
  
  <pre><code>/* GET home page. */
  router.get('/', function(req, res, next) {
    req.session.user = "12345";
    res.render('index', { title: 'MyApp' });
  });
  </code></pre>
  
  <p>But could not read value inside <code>genid</code> function due to value <code>undefined</code>.
  So, my question is:</p>
  
  <p>Can I generate session id with <code>uuid</code> of any model like <code>user-&gt;uuid</code>? If yes, how can I do it?</p>
  
  <p>Thanks, any help will be appreciated.</p>