Node.js + express.js + socket.io authorization: no cookies

I am having trouble getting cookie data from my socket.io authorization.

io.configure(function() { io.set('authorization', function (data, cb) { console.log(data); // data.headers.cookie <-- should be the cookie }); }); 

So he prints:

 { headers: { host: 'frisr.dk:1000', connection: 'keep-alive', origin: 'http://frisr.dk', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2', accept: '*/*', referer: 'http://frisr.dk/', 'accept-encoding': 'gzip,deflate,sdch', 'accept-language': 'da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4', 'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' }, address: { address: '80.71.135.24', port: 53549 }, time: 'Sun Nov 06 2011 22:34:12 GMT+0000 (UTC)', query: { t: '1320610986125' }, url: '/socket.io/1/?t=1320610986125', xdomain: true, secure: undefined, issued: 1320618852796 } 

you can check the code here: http://frisr.dk

Why is cookie unavailable?

+6
source share
4 answers

You have a gander on your host. What is your client connecting to? I had a problem with expressjs 3. * and Socket.io, but I sent my client to connect:

io.connect(127.0.0.1);

Instead

io.connect('localhost');

Now to crack a wonderful new parsing procedure.

https://groups.google.com/forum/?fromgroups=#!topic/express-js/II3eIM9HHQY

+7
source

A cookie is not set during an acknowledgment request. The cookie is probably already set up for different servers / domains and therefore on your second server it exists in your data object. Check if cookie is set for this domain in firebug ot smth.

+1
source

See this question regarding socket.io and authentication. In fact, there are really a bunch of questions on this topic :)

0
source

You looked, there is no cookie header, something like

 cookie: sid=whatsoever 

I think something is wrong in the app.js. configuration file If you did not create your application using the $ express --sessions myApp in the root folder of the application, you will not have a cookie in the headers.

To make sure you have cookies, try looking at the app.js configuration:

 app.configure(function(){ app.set('port', process.env.PORT || 8080); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); //this snippet here app.use(express.cookieParser('your secret here')); app.use(express.session()); //end of snippet app.use(app.router); app.use(require('less-middleware')({ src: __dirname + '/public' })); app.use(express.static(path.join(__dirname, 'public'))); }); 
0
source

Source: https://habr.com/ru/post/900899/


All Articles