jfromaniello / passport.socketio

access passport.js authenticated user information from socket.io connection
655 stars 81 forks source link

Refresh socket.request.user when user authenticates #132

Open alexwohlbruck opened 7 years ago

alexwohlbruck commented 7 years ago

As of right now, when an unauthenticated user goes my site and then logs in, the socket.request.user variable remains empty for each emit afterward, unless the page is refreshed (Using an Angular single page app). How do I get the updated data when a user logs in or out?

zerefel commented 7 years ago

I've had this issue for quite some time as well. A clean solution would be greatly appreciated, as this makes this library not the best choice for SPAs.

Sannis commented 7 years ago

ж Ихва

On Mar 26, 2017 4:13 AM, "Alex Wohlbruck" notifications@github.com wrote:

As of right now, when an unauthenticated user goes my site and then logs in, the socket.request.user variable remains empty for each emit afterward, unless the page is refreshed (Using an Angular single page app). How do I get the updated data when a user logs in or out?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jfromaniello/passport.socketio/issues/132, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEuN6V_ERGRPLUW-KbIeUHjWiPhz9_uks5rpbu4gaJpZM4MpRrw .

jfromaniello commented 7 years ago

Make sure you initiate a new socket.io conneciton after the user logins.

In new versions of socket.io:

// new socket.io:
var socket = io.connect(socketUrl,{'forceNew': true });

// old socket.io:
var socket = io.connect(URL, {'force new connection': true });

You need to force a new, otherwise socket.io reuses the connection initiated before authentication.

alexwohlbruck commented 7 years ago

Okay, that works. Should I also disconnect after they sign out?

savoiebz commented 6 years ago

Hello team,

I know this issue is old but I would appreciate if we could give it a bit more attention.

Could it be possible to integrate a method bound to the socket that updates the stored values of the session user?

Currently, PasseportJS uses the req.login function that allows you to update the user informations bound to the request.user object.

It is very usefull in a SPA when a user goes to his settings and updates important information. Don't need to reload browser or anything.

I believe this library would profit very positively from a similar approach.

-Thank you

savoiebz commented 6 years ago

Litle update:

After looking online if there was a method bound directly to socket.io to refresh the handshake I found this:

socket.handshake.session.reload(function(err) {
  // Session is refreshed here and socket.request.user values are updated, 
  // if they previsouly changed in the PasseportJS session
});