Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access express.session.MemoryStore via socket.io objects?

I am doing this in a login function

app.post('/teacherlogin', function(request, response) {
    var username = request.body.username;
    var password = request.body.password;
    con.query('SELECT t_id from login_teacher where username="'+username+'" and password="'+password+'"',function(err,results){

       if(results.length > 0) {

           request.session.regenerate(function(){
              request.session.user = username;
              request.session.type = 'teacher';
              request.session.id = results[0].t_id;
              response.redirect('/teacherhome');
          });

       } else {

          response.redirect('teacherlogin');
       }

    });

});

now I want to emit the 'id' and 'type' I have stored to the session object. How should I do this? I have read this article but being inexperienced I am facing difficulty in using it. I have used it in my code

var MemoryStore = express.session.MemoryStore;
var sessionStore = new MemoryStore();
app.use(express.bodyParser());
app.use(express.cookieParser('secret text'));
app.use(express.session({
    store: sessionStore,
    secret: 'secret', 
    key: 'express.sid'}
));

and

var Session = require('connect').middleware.session.Session;
io.set('authorization', function (data, accept) {
    if (data.headers.cookie) {
        data.cookie = require('cookie').parse(data.headers.cookie);
        data.sessionID = data.cookie['express.sid'].split('.')[0];
        console.log('data.sessionID "'+data.sessionID);
        data.sessionStore = sessionStore;
        sessionStore.get(data.sessionID, function (err, session) {
           if (err || !session) {
               accept('Error', false);
           } else {
               data.session = new Session(data, session);
               accept(null, true);
           }
        });
    } else {
      return accept('No cookie transmitted.', false);
    }
});

I am not getting any thing in the session object. I tried to log the contents of the sessionStore and it seems to be empty! Does that mean the information I am storing in the session isn't being stored in the sessionStore? If yes, what should I do to store it there? and if it is stored there why isn't the sessionStore.get function unable to find it?

like image 566
newbie Avatar asked Nov 10 '13 12:11

newbie


1 Answers

I am not sure if you're still working on this, but you can access session data with just a MemoryStore. After all how else would Express use it if it didn't work? :)

A simple way to demonstrate MemoryStore working is this:

var express = require("express")
  , app = express()
  , sessionStore = new express.session.MemoryStore();

// middleware
app.use(express.cookieParser());
app.use(express.session({store: sessionStore, secret: "mysecret"}));

// any endpoint sets a cookie
app.get("/", function(req,res) {
  res.send('ok');
});

// This endpoint reveals it
app.get("/session", function(req, res){
  sessionStore.get(req.sessionID, function(err, data) {
    res.send({err: err, data:data});
  });
});

app.listen(3000);

Hitting / followed by /session results in a response of:

{
  "err": null,
  "data": {
    "cookie": {
      "originalMaxAge": null,
      "expires": null,
      "httpOnly": true,
      "path": "/"
    }
  }
}

I suspect your issue may be how you are getting the sessionID from the socket, but it is definitely possible to extract a session from a MemoryStore. Also, remember that restarting the Express server will destroy all of your sessions so you'll need a new cookie after each restart.

like image 63
Rob Riddle Avatar answered Nov 15 '22 23:11

Rob Riddle