Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session cookies do not change using ajax and nodejs server

I want to change a session cookie after an asynchronous request but no matter what I tried I keep failing. My request is as follows:

$.ajax({
    type: "POST",
    url: "/setStatus",
    data: { userId : _userId, token: _token, tokenSecret : _tokenSecret, service : service, loggedIn : _loggedIn, authorized : _authorized },
    xhrFields: { withCredentials: true },
    crossDomain: true
}).done(function(reply) { alert('finished'); });

Setting the session variables on the server.

exports.setStatus = function(req, res)
{
    req.session.userId = req.body.userId;
    req.session.token = req.body.token;
    req.session.tokenSecret = req.body.tokenSecret;
    req.session.service = req.body.service;
    req.session.loggedIn = req.body.loggedIn;
    req.session.authorized = req.body.authorized;

    res.header('Access-Control-Allow-Credentials', 'true');
    res.writeHead(200);
};

The setting on the server are as follows:

app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat', store: new RedisStore({ host: 'localhost', port: 3000, client: dbcon })}));
app.use(express.methodOverride());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);

I forgot to mention that on simple requests the session cookies change as expected. Any ideas?

like image 249
scarleth ohara Avatar asked May 24 '13 15:05

scarleth ohara


People also ask

Can AJAX calls set cookies?

Yes, you can set cookie in the AJAX request in the server-side code just as you'd do for a normal request since the server cannot differentiate between a normal request or an AJAX request.

Does AJAX need cookies?

We are required to set cookies with AJAX requests or in such a way that any AJAX request sends those cookies to the server. One thing to note here is that every AJAX request made to any remote server automatically sends all our cookies to that very server without us having to do anything.

What is session cookie in Nodejs?

Cookies and sessions make the HTTP protocol stateful protocol. Session cookies: Session cookies are the temporary cookies that mainly generated on the server-side. The main use of these cookies to track all the request information that has been made by the client overall particular session.

How do you set an express session cookie?

var cookieSession = require('cookie-session') var express = require('express') var app = express() app. use(cookieSession({ name: 'session', keys: ['key1', 'key2'] })) // Update a value in the cookie so that the set-cookie will be sent. // Only changes every minute so that it's not sent with every request. app.


1 Answers

You should call req.session.save() after the modifications if you are doing it with ajax.

exports.setStatus = function(req, res)
{
    req.session.userId = req.body.userId;
    req.session.token = req.body.token;
    req.session.tokenSecret = req.body.tokenSecret;
    req.session.service = req.body.service;
    req.session.loggedIn = req.body.loggedIn;
    req.session.authorized = req.body.authorized;
    req.session.save(); // This saves the modifications

    res.header('Access-Control-Allow-Credentials', 'true');
    res.writeHead(200);
};
like image 51
DeadAlready Avatar answered Oct 17 '22 09:10

DeadAlready