Store session data using MemoryStore in Node, simi

2019-03-30 06:25发布

问题:

Question:

Is it possible to store session data similar to $_SESSION['somedata'] = "Hello" in php?

Here is my code so far:

Creating the memory store

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

Express Config

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
    store: sessionStore,
    secret: 'secret', 
    key: 'express.sid'}));
  app.use(require('stylus').middleware({src: __dirname + '/public'}));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

Parsing the cookie to get the session id on handshake authorization

var parseCookie = require('connect').utils.parseCookie;

io.set('authorization', function (data, accept) {
  if (data.headers.cookie) {
    data.cookie = parseCookie(data.headers.cookie);
    data.sessionID = data.cookie['express.sid'];

    sessionStore.get(data.sessionID, function (err, session) {
      if (err) 
      {
          accept(err.message, false); //Turn down the connection
      } 
      else
      {
          data.session = session; //Accept the session
          accept(null, true);
      }
    });
  } else {
     return accept('No cookie transmitted.', false);
  }
});

Storing 'loggedin' when the password and username are 'admin'

io.sockets.on('connection', function (socket) {

  socket.on('details', function(data){
    console.log("Details: " + data.u + data.p);
    if(data.p == "admin" && data.u == "admin")
    {
      //Add the logged in field to the session
    }
  });
});

If the user is logged in redirect them to the home page

app.get(navigation.login.uri, function(req, res){
  if(req.session.loggedin)
  {
    res.redirect('/home');
  }
  else
  {
    res.render('login', {
      title: navigation.login.title,
      navigation: navigation
    });
  }
});

When I try to use:

req.session.loggedIn

The value is undefined. Could this be a storage problem, or am I accessing it incorrectly?

回答1:

You're close - just missing a couple of things.

The information that you are gathering in the socket authorization function is accessible via socket.handshake in your connection handler.

So, what I think you want is:

io.sockets.on('connection', function (socket) {

  socket.on('details', function(data){
    console.log("Details: " + data.u + data.p);
    if(data.p == "admin" && data.u == "admin")
    {
      //Add the logged in field to the session
      socket.handshake.session.loggedIn = true;  // Set the new session var
      socket.handshake.session.save();           // Save the modified session
    }
  });
});

Don't forget to save the session after modifying it otherwise it never gets back into the store.



回答2:

This worked for me - you basically just have to set a key in the sessionStore:

io.sockets.on('connection', function (socket) {
     socket.on('details', function(data){
          console.log("Details: " + data.u + data.p);
          if(data.p == "admin" && data.u == "admin")
          {
               // set the value
               sessionStore.loggedIn = true;
               // get the value
               console.log(sessionStore.loggedIn);
          }
     });
});

I tried it on a website with some subpages, the value was consistent. A forced reload or closing the browser resets it.