Meteor React: What is the simplest way to ensure a user is logged in before rendergin a page?

69 Views Asked by At

Situation:

  • I'm using React in a Meteor app.
  • My packages for authentication: accounts-password; useraccounts:core
  • I also use the gadicc:blaze-react-component to render the {{> atForm }} template in my react component =><Blaze template="atForm" />

Problem:

I don't know what should be the proper syntax to secure a specific page as described in the useraccounts guide but with react.

Lets suppose that I want to secure <Mypage /> imported from './mypage.jsx'

How can I call {{> ensureSignedIn template="myTemplate"}} and reaplace "myTemplate" by <Mypage /> ?

I tried <Blaze template="atForm" template={Mypage} /> without success...

Is it even possible to make something like this ?

2

There are 2 best solutions below

0
Nenu On BEST ANSWER

I found a solution:

With kadira:flow-router Meteor's package, using react-layout and useraccounts:flow-routing, I can do something like that:

FlowRouter.route('/private', {
  triggersEnter: [AccountsTemplates.ensureSignedIn],
  action: function() {
    ReactLayout.render(Mypage, {myprops: myprops.value});
  }
});

It seems that it all happens into triggersEnter: [...] key.

More details here.

0
none On

You can check Meteor.userId() in ComponentWillMount(), set state and check it in render(). Or use some kind of checkings in your router