4

I am currently using express and express-session to save information. If I save a session variable like this in file 'routes/login.js'

req.session.user = "User1"

How do I then access that same variable from 'routes/index.js' because currently if I just do

console.log(req.session.user)

it will log undefined but if I do the same thing from inside 'routes/login.js' it will log "User1"

EDIT:

Inside 'routes/login.js' file will log "User1"

 var express = require('express');
    var redis = require('redis');
    var session = require('express-session');
    var RedisStore = require('connect-redis')(session);

    var router = express.Router();
    var client = redis.createClient();

    var store = new RedisStore({host: 'localhost', port: 6379, client: client, ttl: 260});

    router.use(session({
         resave: true,
         saveUninitialized: true,
         secret: "secret",
         store: store
        }));

    router.get('/user', function(req, res) {
        req.session.test = "User1";
        res.send(req.session.test); //Logs "User1"
    });

module.exports = router;

Inside 'routes/index.js' file will log undefined

var express = require('express');
var router = express.Router();
var config = require('../config/config')

/* GET home page. */
router.get('/', function(req, res, next) {
    console.log(req.session); //Logs undefined
});

module.exports = router;
Undying
  • 141
  • 3
  • 14
  • Could you include the code you use to create/register the `express-session` middleware and the relevant routes, i.e. the calls to `app.use` or `app.get` or whatever you're using? – skirtle Oct 07 '17 at 21:35
  • 1
    You only seem to be using the `express-session` middleware within your login router, so requests that don't go through that router won't have a session. Typically you'd register the middleware higher up, so that it's in the chain for all requests that need sessions. – skirtle Oct 07 '17 at 21:52
  • An alternative to moving it 'higher up' might be to put the relevant code in a separate file and `require` it into every route that needs it. – skirtle Oct 07 '17 at 21:54
  • When you say relevant code in this scenario would that mean the session middleware and redis store? – Undying Oct 07 '17 at 21:56
  • If I use the middleware with the app rather than the router will it be accessible for all routers? – Undying Oct 07 '17 at 21:57
  • why dont you use middlewares to write a "before" request hook to attach user session to each request? – arvin karimi Oct 07 '17 at 22:05

2 Answers2

1

I was able to solve my problem by using the express session middle ware with my app and then accessing it from routes.

Inside app.js

app.use(session({
     resave: true,
     saveUninitialized: true,
     secret: "secret",
     store: store
 }));

Now I can save a variable to request.session and it will persist through both route files.

Undying
  • 141
  • 3
  • 14
0

use express-session in your entry point like app.js where your app is actually initialized

Before accessing routes in app.js put the following code

app.use(session({
         resave: true,
         saveUninitialized: true,
         secret: "secret",
         store: store
        }));

Remember to maintain the order.

Dev Matee
  • 5,525
  • 2
  • 27
  • 33