Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access server session information on the client without reload or extra request

I authenticate server-side with express.session. The normal way of communicating that session information client-side is to rerender the page with new HTML generated server-side, but this requires a reload.

I have a one page app and want to avoid reloading. Is there a way to access information about the connected user client-side? Is this information stored in a cookie? How can I access it without reloading the page or making an extra server request?

like image 850
Randomblue Avatar asked Jan 07 '12 15:01

Randomblue


2 Answers

The only way to do this would be to either include the information in the initial server response (perhaps a dynamically generated JS object embedded in the HTML) or by making a second request as alessioalex suggested.

It not possible to get information from the server WITHOUT talking to the server...

like image 196
Nick Mitchinson Avatar answered Oct 19 '22 09:10

Nick Mitchinson


As @stagas suggested, you can create a custom route with Express, such as /user and return session data (user details) using JSON. That way you wouldn't need to refresh the page.

Example:

app.get('/user', function (req, res) {
  res.json(req.session.user);
});
like image 4
alessioalex Avatar answered Oct 19 '22 10:10

alessioalex