Send Meteor.user() on initial load

Is there any reason why this doesn’t happen already via the Accounts package?
I previously used fast-render but now that package is so old it crashes my app when I add it.

Does anyone know if there is another package that does something similar?

My issue is I am using React Router and I have protected routes for logged in users only & admin routes for admin users.
The routes are loaded on Meteor.startup() and the Meteor.user() object is not available at startup and those routes bounce the user to the home page via <Redirect>.

Also happy to hear of other innovative ways around the issue…

Try to handle all your routes in a different file or component, u can call it for example App. After that u just have to render <App /> inside your Meteor.sartup(). If u want to load user collection, u can do it with withTracker to create a container inside App.

import....

const App = props => (
  <Router>
    ...
    <Switch>
     ...
     <Route path='/test' component={Test} {...props} />
     ...
    </Switch>
  </Router
);

// propTypes

export default withTracker(() => {
  ...  
  const user = Meteor.user();
  ...
  
  return {
    user,
    ...,
  }
})(App);

It’s just an example, maybe there is a better way to do that.

The reason is that only the user’s _id is stored in the session. All other data is destroyed on refresh and re-fetched from the server.

There are 2 ways to solve it:

  1. Store user data required to do your authorization as part of a persistent session. This requires you to manually remove that data on logout. Actually there is a great package for that although i’m not sure if they still maintain it: https://atmospherejs.com/u2622/persistent-session. This allows you to set session data for as long as the user is authenticated.
  2. Wait with your system until the userData subscription has finished. This means that you subscribe to the data, store a status of this subscription somewhere and show a loading icon until the subscription finished. (Note that you can do this by tracking Meteor.user() reactively and base that status on that, but when something goes wrong you will not be able to remove the spinner)

Ah I think that is the answer.

I am rendering my routes on startup - I will just render them in a component further down the line where I have a bit more control.

1 Like

Yeah that worked great.
I just put a simple component in the startup which loaded my main component with routes and all.
I will post my code later in case anyone else wants to copy.

2 Likes

Here is a Gist of the code as I have implemented it. Would love feedback if I can improve anything.

2 Likes

https://atmospherejs.com/staringatlights/fast-render

1 Like

Thanks, I liked the idea of fast-render, but I think the alternative solution is better. Fits in nicer with the meteor flow.

Did you use this with SSR?