Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  54] [ 1]  / answers: 1 / hits: 23399  / 13 Years ago, wed, may 4, 2011, 12:00:00

I'm having a problem with sessions, where sometimes the session variable I just set
is undefined on the next page request. I typically have to go through the flow
again in order to properly set the variables.



I can confirm that I'm not trying to set the session variables to undefined; they have a legit value.



In my app, users move from /twitter/connect/ to /twitter/callback/. The former retreives some oauth data from twitter, the latter logs the user into twitter.



/twitter/connect/ is simple:



app.get('/twitter/connect/?', function(req, res){
consumer().getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
if (error){
// error handling here
} else {
req.session.oauthRequestToken = oauthToken;
req.session.oauthRequestTokenSecret = oauthTokenSecret;

// if I console.log the two session variables above
// they have the proper values.

res.redirect(https://twitter.com/oauth/authorize?oauth_token=+req.session.oauthRequestToken);
}
});
});


After that, twitter sends them back to /twitter/callback/:



app.get('/twitter/callback/?', function(req, res){
console.log(req.session.oauthRequestToken);
console.log(req.session.oauthRequestTokenSecret);

// more often than not, the two variables above are
// undefined. but not always. usually on the first
// pass, never on the second.
});


I have no idea what's going on, I can confirm that the session variables are being set properly, they just aren't holding their value in between page requests, but only the first time.



This is how I'm creating my server:



app.configure('development', function(){
app.use(express.cookieParser());
app.use(express.session({ secret:'yodawgyo' }));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.logger());
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.set('view options', {
open: '{{',
close: '}}'
});
});


I just have a dev environment for now. I have Node 0.5.0-pre installed, but saw this issue on 0.4.1 as well. I'm using express 2.3.2.



Any help is much appreciated.


More From » node.js

 Answers
265

Just struggled with the same problem and got it resolved.
The simple answer is to call



 Session.save() 


explicitly, if you cannot reply on express-session to call it for you at the end of the response.



Reference


[#92408] Tuesday, May 3, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
desiraeleandrah

Total Points: 202
Total Questions: 111
Total Answers: 115

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;