Sessions not working

2,076 views
Skip to first unread message

express-noobage

unread,
Jan 8, 2011, 1:45:42 AM1/8/11
to Express
Hey guys, I'm trying to get started with express, but I ran into a
wall today. I can't get sessions to work for some reason, session vars
are always undefined when the page reloads. I've tried a number of
memory stores as well, please help!

var express = require('express');
var app = express.createServer();

var MemoryStore = require('connect/middleware/session/memory');
app.use(express.bodyDecoder());
app.use(express.cookieDecoder());
app.use(express.session({ store: new MemoryStore({ reapInterval: 60000
* 10 }) }));

app.get('/', function(req, res){
req.session.visitCount = req.session.visitCount ?
req.session.visitCount + 1 : 1;
res.send('You have visited this page ' + req.session.visitCount +
' times');
});

app.listen(4000);

vision media [ Tj Holowaychuk ]

unread,
Jan 8, 2011, 6:32:22 PM1/8/11
to expre...@googlegroups.com
looks ok, looks like the example I have (which does work for me). on a side note you can use express.session.MemoryStore which is the same thing. Can I ask which version of node? 0.3.x may break


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.




--
Tj Holowaychuk
Vision Media
President & Creative Lead

Jay Fallon

unread,
Feb 9, 2011, 4:04:35 PM2/9/11
to Express
Is this related to:

Error: Cannot find module 'connect/middleware/session/memory'

I'm using v0.2.6 and it's throwing me this error when I do:

var MemoryStore = require('connect/middleware/session/memory');

app.use(express.session({store: MemoryStore(
{reapInterval: 60000 * 10}
)}));

- Jay

On Jan 8, 6:32 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-
media.ca> wrote:
> looks ok, looks like the example I have (which does work for me). on a side
> note you can use express.session.MemoryStore which is the same thing. Can I
> ask which version of node? 0.3.x may break
>
> On Fri, Jan 7, 2011 at 10:45 PM, express-noobage <skunkreco...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > Hey guys, I'm trying to get started with express, but I ran into a
> > wall today. I can't get sessions to work for some reason, session vars
> > are always undefined when the page reloads. I've tried a number of
> > memory stores as well, please help!
>
> > var express = require('express');
> > var app = express.createServer();
>
> > var MemoryStore = require('connect/middleware/session/memory');
> > app.use(express.bodyDecoder());
> > app.use(express.cookieDecoder());
> > app.use(express.session({ store: new MemoryStore({ reapInterval: 60000
> > * 10 }) }));
>
> > app.get('/', function(req, res){
> >    req.session.visitCount = req.session.visitCount ?
> > req.session.visitCount + 1 : 1;
> >    res.send('You have visited this page ' + req.session.visitCount +
> > ' times');
> > });
>
> > app.listen(4000);
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Express" group.
> > To post to this group, send email to expre...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > express-js+...@googlegroups.com<express-js%2Bunsubscribe@googlegrou ps.com>
> > .

vision media [ Tj Holowaychuk ]

unread,
Feb 9, 2011, 4:44:00 PM2/9/11
to expre...@googlegroups.com
require('connect').session.MemoryStore

To unsubscribe from this group, send email to express-js+...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

Jay Fallon

unread,
Feb 9, 2011, 5:00:58 PM2/9/11
to Express
Thanks. Also had to add secret option due to

Error: session() middleware requires the "secret" option string for
security

app.use(express.session({secret: 'your_secret_is_safe_here', store:
MemoryStore(
{reapInterval: 60000 * 10}
)}));

On Feb 9, 4:44 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-

fatjonny

unread,
Feb 10, 2011, 12:10:48 PM2/10/11
to Express
I am having issues with Sessions not working consistently and I am not
sure if it is something I am doing or not, but they seemed to work
consistently before I updated a few times.

I am using node 0.2.6, express 1.0.7 and connect 0.5.9.

When I login and try to set a user object on my session it works about
2/3rds of the time. The other times, the session object seems to get
cleared. My code (it is being set on a user manager object which is
what 'that' refers to) looks like:

that.loginPOST = function( req, res ) {
that.authenticate( req.body.username, req.body.password,
function( err, user ) {
if( user ) {
req.session.regenerate( function() {
req.session.user = user;
res.redirect('home');
});
} else {
req.session.error = 'Authentication failed.';
res.redirect('back');
}
});
};

that.logoutGET = function( req, res ) {
req.session.destroy( function() {
res.redirect('home');
});
};

Usually the best way to repeat it is to log in, log out, log in, log
out, then log in again. On the third log in the user variable in the
session does not get stored. I have printed out the user object in the
callback I am passing to regenerate() and it is always set correctly.
However, when 'home' gets rendered, the session object only sometimes
has it set properly.

I have tried explicitly setting the store to MemoryStore and also only
setting one variable on session rather than setting an object and it
still is not set when rendering the home page.

Am I doing something wrong?

Jon

vision media [ Tj Holowaychuk ]

unread,
Feb 10, 2011, 12:38:24 PM2/10/11
to expre...@googlegroups.com
hmm it looks ok, i will try and reproduce this soon

fatjonny

unread,
Feb 10, 2011, 1:29:04 PM2/10/11
to Express
I appreciate it! Here is the smallest test case I could make:

https://gist.github.com/821043

Surprisingly trying to set req.session.user =
{ name:req.body.username }; ...always fails for me right now, but
'test' shows up the first two times.

Jon

On Feb 10, 11:38 am, "vision media [ Tj Holowaychuk ]" <t...@vision-

vision media [ Tj Holowaychuk ]

unread,
Feb 10, 2011, 7:57:56 PM2/10/11
to expre...@googlegroups.com

jonny

unread,
Feb 10, 2011, 8:03:27 PM2/10/11
to expre...@googlegroups.com
Hmm. That doesn't work for me. I am guessing that I messed up my
configuration otherwise this would be a much bigger problem for other
people. :)

Thanks! I will re-install everything tonight and see if that fixes it.

Jon

vision media [ Tj Holowaychuk ]

unread,
Feb 10, 2011, 8:15:32 PM2/10/11
to expre...@googlegroups.com
people do seem to have session issues once and a while, although I cannot ever seem to reproduce them

Casey Banner

unread,
Feb 10, 2011, 8:24:17 PM2/10/11
to expre...@googlegroups.com
Make sure your server time is set correctly. I recently wasted a few
hours debugging sessions and cookies, only to find my server time was
off by several hours from the client time. This was causing cookies to
get cleared instantly.

On Thu, Feb 10, 2011 at 8:15 PM, vision media [ Tj Holowaychuk ]

vision media [ Tj Holowaychuk ]

unread,
Feb 10, 2011, 8:55:49 PM2/10/11
to expre...@googlegroups.com
right, that too. we just talked about that today here actually haha

fatjonny

unread,
Feb 11, 2011, 12:42:04 AM2/11/11
to Express
Ok, I figured out what is causing my problem. I uninstalled and
reinstalled express and connect and I got the same problem with your
code and mine. I took some time to step through the code in a debugger
and what seems to be happening is that in connect/middleware/
session.js where it checks the fingerprint, that it fails that check
and generates a new session which obviously doesn't transfer the old
session data.

The check ( parts[1] !== hash(parts[0]) ) is failing on what I am
assuming are supposed to be equal terms but a character is somehow
getting converted differently. For instance, two cases I saw were:

>>>
parts[1]
ySNKsSR9Pz3g1Cc4FMbc aMGMbFYy6J/IsVOrO/Ir3Y
>>>
hash( parts[0] )
ySNKsSR9Pz3g1Cc4FMbc+aMGMbFYy6J/IsVOrO/Ir3Y

and...

>>>
parts[1]
QxQQ3PcPvDhH3RPgjsO5oMsCQLEY Pj7HwbEp7TKDnU
>>>
hash( parts[0] )
QxQQ3PcPvDhH3RPgjsO5oMsCQLEY+Pj7HwbEp7TKDnU

The space is becoming a plus in the hash but not in the first part of
the check. I see in the hash function in session.js that =, *, and $
are getting replaced, does + need to get replaced as well?

Oh, and in case it matters I am running this locally on my Macbook
with 10.6 and viewing it in both Chrome and Safari (since I saw
somewhere that the session id might be provided by the browser).

Jon


On Feb 10, 7:55 pm, "vision media [ Tj Holowaychuk ]" <t...@vision-
media.ca> wrote:
> right, that too. we just talked about that today here actually haha
>
>
>
>
>
>
>
> On Thu, Feb 10, 2011 at 5:24 PM, Casey Banner <kcban...@gmail.com> wrote:
> > Make sure your server time is set correctly. I recently wasted a few
> > hours debugging sessions and cookies, only to find my server time was
> > off by several hours from the client time. This was causing cookies to
> > get cleared instantly.
>
> > On Thu, Feb 10, 2011 at 8:15 PM, vision media [ Tj Holowaychuk ]
> > <t...@vision-media.ca> wrote:
> > > people do seem to have session issues once and a while, although I cannot
> > > ever seem to reproduce them
>
> > > On Thu, Feb 10, 2011 at 5:03 PM, jonny <fatjo...@gmail.com> wrote:
>
> > >> Hmm. That doesn't work for me. I am guessing that I messed up my
> > >> configuration otherwise this would be a much bigger problem for other
> > >> people. :)
>
> > >> Thanks! I will re-install everything tonight and see if that fixes it.
>
> > >> Jon
>
> > >> On Thu, Feb 10, 2011 at 6:57 PM, vision media [ Tj Holowaychuk ]
> > >> <t...@vision-media.ca> wrote:
> > >> > hmm this works for me:
> > >> >https://gist.github.com/c76716ef0717c1fe752a
>
> > >> > On Thu, Feb 10, 2011 at 10:29 AM, fatjonny <fatjo...@gmail.com>
> > >> >> > > To post to this group, send email to expre...@googlegroups.com
> > .
> > >> >> > > To unsubscribe from this group, send email to
> > >> >> > > express-js+...@googlegroups.com.
> > >> >> > > For more options, visit this group at
> > >> >> > >http://groups.google.com/group/express-js?hl=en.
>
> > >> >> > --
> > >> >> > Tj Holowaychuk
> > >> >> > Vision Media
> > >> >> > President & Creative Lead
>
> > >> >> --
> > >> >> You received this message because you are subscribed to the Google
> > >> >> Groups
> > >> >> "Express" group.
> > >> >> To post to this group, send email to expre...@googlegroups.com.
> > >> >> To unsubscribe from this group, send email to
> > >> >> express-js+...@googlegroups.com.
> > >> >> For more options, visit this group at
> > >> >>http://groups.google.com/group/express-js?hl=en.
>
> > >> > --
>
> ...
>
> read more »

fatjonny

unread,
Feb 11, 2011, 1:20:04 AM2/11/11
to Express
The problem in issue 196 is this problem. It happens often for me on
my configuration for some reason.

https://github.com/senchalabs/connect/issues#issue/196

Jon
> ...
>
> read more »

abdullah mohamed mohamed abdelrhim

unread,
Feb 12, 2011, 7:28:29 AM2/12/11
to expre...@googlegroups.com
I TRY ALOT OF THINGS BUT IT DOESNOT WORK FOR ME !!

To unsubscribe from this group, send email to express-js+...@googlegroups.com.

fatjonny

unread,
Feb 15, 2011, 9:53:09 PM2/15/11
to Express
Tj,

I see that a fix was included in the 0.5.10 release. I ran through
quite a few sessions and I am not getting the error to happen anymore.
Thanks!

Jon

On Feb 12, 6:28 am, abdullah mohamed mohamed abdelrhim
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages