How to wait for Meteor.user() and redirect if undefined from within the container?

I want restrict non logged in users from viewing some pages in my app by redirecting them to the login page. I use react, flow router and the react komposer. The problem is, Meteor.user() first gives undefined and even-though the user is logged in, he gets re-directed into the login page. How can I fix this?

1 Like

Im no expert but was struggling with the Meteor authentication state and React for a while.

My solution after realising we are essentially talking about reacting to some kind of global state was to use Redux and React Router.

By using a resolver which incorporates the Meteor login logic one is able to keep the logged in state easily managed.

One you enter the React Router world there are lots of redirect options I used Redux Auth Wrapper which will redirect on entry if not logged in.

Looking at the many of the pros here they seem to favor component level logic as in
authState ? ( Render view for user ):(don't)
but I wanted the redirection logic.

I realise this is quite a tangent from what u are currently using but I saw this was a day old so just wanted to show some support.

If you want to see my take on it the public repo is https://github.com/atapp/apollo-redux-meteor-userdemo this has some extra functionality to do with data sourcing with apollo and postgre, but you can have a look at the resolvers and actions if you chose this direction.

I have also seen other takes on Meteor authentication in Redux by using tracker to make the resolver reactive, however I prefer the dispatch action.

Like I said I am a beginner so this is unlikely to be best practice. Good luck.

Install fast-render, the default settings of fast-render deliver the user document for immediate login on load.

You could also Use Meteor.loggingIn() it will return true when a user is in the process of logging in.

1 Like