SocketCluster / socketcluster-server

Minimal server module for SocketCluster
http://socketcluster.io/
MIT License
108 stars 50 forks source link

Parsing data from socketcluster to express #27

Closed anywhere3d closed 6 years ago

anywhere3d commented 6 years ago

Hi Jonathan! I am using SocketCluster as stand-alone server and have attached the express httpServer on it. Express use the express-Handlebars as render engine with raw html files. The client login via a "login" socket emit event to SCserver. On server-side the "login" socket listener make a query to the mongo database with the username or email, find the user in Users collection and authenticate the client by setting the AuthToken object. Then send it back to the client with the "login" event response. In client-side when the socket status is authenticated, redirect the client to the "/home" page: location.pathname = "/home";

The problem: I need some of the user data to set in local variables in the "home" view before the handlebars engine render it and send it to the client but: I had these data at setAuthToken() authentication time, but that was while the user was in "/login" page. Now the user is about to receive and load the "/home" page so need to send a selector to the server to query the database to send again the user data to server but before express-handlebars engine render the "home" view. I have the selector in the socket.authToken but must send it to express before it render the html. The authentication became in a socket.on("login", function(credentials, res){ ... }); block and the rendering will take place in a app.get('/home', function(req, res){ ... }); block. I look around about a solution on how to parse the user data from the socket block to the express block, but not found something relative. Can you help me please?

anywhere3d commented 6 years ago

SOLVED: I followed your instructions here . I attached in the app thescServer.options.authKeyand then created an jwt token using the app.authKey. After that i send it to the client and stored in localStorage under the "socketCluster.authToken" key. Thank you.