Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using session information for server-rendered pages. (Logging in.) #37

Open
chrisdew opened this issue Oct 30, 2014 · 1 comment
Open

Comments

@chrisdew
Copy link

I've based a project around your react-quickstart structure.

I'm developing a login system which store the currently-logged-in-user as a logged_in state at the top level element (above the react-router-component pages). See STRML/react-router-component#98 for more details.

I've run into a hurdle. The top-level App class makes an AJAX call to get its logged_in state.

When the AJAX call is made from a browser, the express-session middleware is used to retrieve the sessions's username, and that is returned to the calling browser.

When the page is rendered server-side (because of a page refresh, for example), the AJAX call is made form the server, to the server, and so doesn't have the session details.

I suppose that what I'm looking for is a way to send the request session cookie through to the AJAX calls the server makes to itself when rendering a page server-side.

Is there a way to do this?

Or is there a better login solution which works for both client and server-rendered pages?

@chrisdew
Copy link
Author

I found a solution to this. Instead of getting the logged_in state via an AJAX call, I found that I could simply bake it into the page at server-side render time.

function renderApp(req, res, next) {
  var path = url.parse(req.url).pathname;
  var app = App({path: path, logged_in: req.session.logged_in});
  ReactAsync.renderComponentToStringWithAsyncState(app, function(err, markup) {
    if (err) {
      return next(err);
    }
    res.send('<!doctype html>\n' + markup);
  });
}

I'll leave the issue open for day or two, in case anyone wants to comment on whether this is the best way to handle logging in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant