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?
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...
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With