Session: I can not retreive session value.

Created on 31 Mar 2016  路  8Comments  路  Source: expressjs/session

Hi
I am using express-session for my project
But I can not retrieve session value, because after saving session, it is destroyed automatically.
I am not sure why this happens.
Could you please help me?
Please review following code.
-------app.js------
var express = require('express');
var session = require('express-session');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');
var log = require('loglevel');

require('rootpath')();
var routes = require('config/router');' var app = express();`

app.use(session({ secret: 'SessionSecret' }));
app.use(cors());
app.options('/api/', cors());
routes.setup(app);
-----------------------------------------------

------------------router.js----------------
require('rootpath')();
var api_auth_ctrl = require('app/controller/api/v1/auth.controller');

module.exports.setup = function(app) {
app.post('/api/v1/auth/login', api_auth_ctrl.login);
app.get('/api/v1/auth/info', api_auth_ctrl.userInfo)
}
-----------------------------------------------

--------------------auth.js-------------------
exports.login = function(req, res) {
req.session.email = req.body.email;
.........
res.json({
type: "success"
});
};
exports.userInfo = function(req, res) {
var logged_in_user = req.session.email;
.........
res.json({
user: user
});
};

question

Most helpful comment

i found my problem, so late to answer , but better than never, i was setting my cookies on https that was why i had a problem , thank you.

app.use(session({
  cookie: { secure: true } // i believe this was my problem 
}))

All 8 comments

I have the same problem..

297 me too, help please!

+1

I don't know what to say because I just ran exactly this:

app.js:

var express = require('express');
var session = require('express-session');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cors = require('cors');
var log = require('loglevel');

require('rootpath')();
var routes = require('router.js');
var app = express();

app.use(session({ secret: 'SessionSecret' }));
app.use(cors());
app.options('/api/', cors());
routes.setup(app);
app.listen(8080);

auth.js:

require('rootpath')();
var api_auth_ctrl = require('auth.js');

module.exports.setup = function(app) {
    app.get('/login',function(req,res){
        res.send("<html><body><form method='post'><button type='submit'>POST!</button></form></body></html>")
    });
    app.post('/login', api_auth_ctrl.login);
    app.get('/info', api_auth_ctrl.userInfo)
}

router.js:

exports.login = function(req, res) {
    req.session.email = "an email goes here";//req.body.email;
    res.json({
        type: "success",
    });
};
exports.userInfo = function(req, res) {
    var logged_in_user = req.session.email;
    res.json({
        user: logged_in_user, // changed user to logged_in_user
    });
};

this provided the expected value back to me so something you are leaving out is causing your issue.


Questions:

  • what versions are you using? can you provide your package.json:dependencies?
  • what version of node are you using?
  • are you using a session store? (if not that may be your issue if you are behind a reverse proxy like nginx/haproxy)

Ideas:

  • connecting to a session store & either the database or the session store is not working properly
  • using a browser which might not be saving cookies which this library assumes you can save (there are was of checking if the user is not saving cookies)
  • mismatched variables?
  • multiple instances which are not sharing their sessions because they are not using a session store

Have same problem, session gets cleared all time after any request (no matter get or post):

just clone (clean express4+session):

https://github.com/DeadcatDev/express-session-bug

then

(sudo) npm install

and

node bin/www

Using Postman with get routes:

localhost:3000/
localhost:3000/a
localhost:3000/b

Gives 'cleared' session everytime - see console - even using cors()

i found my problem, so late to answer , but better than never, i was setting my cookies on https that was why i had a problem , thank you.

app.use(session({
  cookie: { secure: true } // i believe this was my problem 
}))

@DeadcatDev if you are using Postman you may have to include the cookie manually. Are you doing that?

i found my problem, so late to answer , but better than never, i was setting my cookies on https that was why i had a problem , thank you.

app.use(session({
  cookie: { secure: true } // i believe this was my problem 
}))

thanks for the answer, just set secure false and it works

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brimsey picture brimsey  路  3Comments

MayasHaddad picture MayasHaddad  路  4Comments

inkquery picture inkquery  路  3Comments

yolapop picture yolapop  路  3Comments

UnderTheMoonspell picture UnderTheMoonspell  路  3Comments