Problem requiring user authentication for route using React Router + React

Hi peeps, I’m having a problem trying to check for special permissions to access a route using the meteor-roles package and React Router. The problem is, I’ve wrote a simpleB onEnter hook to check if the currently logged in user has admin rights to access a route:

var adminArea = function (nextState, replace) {
  if (!Meteor.user() || !Roles.userIsInRole(Meteor.userId(), ['admin'], Roles.GLOBAL_GROUP))

However, this check always fails and redirects me to the homepage, even if the currently logged in user has admin rights. Because the current user has not been fetched from the database yet, and Meteor.user() returns undefined. I’ve also tried to do the same thing from a component but I’m having the same problem. Usually, facing this kind of problem on a more traditional app, I’d provide a callback or listen to some event. How do I get around this situation in this case?

Try this:

function waitForLoginState(done) {
  Tracker.autorun((computation) => {
    if (!Meteor.loggingIn()) {

Like this:

waitForLoginState(() => {
  if (Meteor.user()) {
    // logged in

Thanks a lot! This works perfectly, I’ve had thought about messing with Tracker too but I didn’t know if or how it would work with React Router, I’ll try to dig deeper into this Tracker voodoo.

1 Like

How did you use it in above example?