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?
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.
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.
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.
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.
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);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With