0

I want people to login using a standard loginform and then join a socket.io chatroom if authentication was successful. I dont know how to retrieve the session when setting up the socket.io part. So far I have setup a session using:

app.use(express.cookieParser());
app.use(express.session({secret:'my-no-longer-secret-secret'}));

app.post('/login', function(request, response) {
    // lookup credentials from database
    var login = true;
    if(login) {
        request.session.authorized = true;
        request.session.room = request.body['room'];
        console.log("Sessino authorized: " + request.session.authorized);
        console.log("Session room " + request.session.room);
    }
    else {
        routes.login(request, response);
    }

    routes.index(request, response);
})

I want to be able to retrieve the request.session .someinfo here in the socket part but as I understand it, it is not possible. Instead I have to use a cookieSession but I am not sure how to do that.

Thank you in advance

Todilo
  • 1,256
  • 3
  • 19
  • 38
  • 1
    Does this answer your question: http://stackoverflow.com/a/16066761/2210128 – Bret Copeland May 13 '13 at 07:15
  • 4
    Take a look at [session.socket.io](https://github.com/functioncallback/session.socket.io/) (and its [example](https://github.com/functioncallback/session.socket.io/blob/master/example/server.js)). – robertklep May 13 '13 at 07:23
  • I was actually just coming back to suggest the same thing for a prebuilt solution, so I'll just second @robertklep's comment instead. – Bret Copeland May 13 '13 at 07:55
  • That seems simple enough, the example was perfect, it does feel like I have to make only a minor change. Thanks. – Todilo May 13 '13 at 14:19
  • I actually end up using Bret Copeland's post. The session.socket.io seemed like too much and well the link-answer worked. – Todilo May 13 '13 at 19:34
  • 1
    Hi i found this post very usefull: http://stackoverflow.com/questions/8754849/socket-io-authentication-after-socket-established – Abdul Aleem Oct 29 '13 at 15:57

0 Answers0