Search code examples
meteorrolesflow-router

Flow router and filtering roles - Meteor


I'm dealing with one problem more than 10 days now, and I don't know what to do with it, so I hope that I'll get my solution here.

I have two roles 'admin' and 'user': the first user is added as the admin thanks to the alanning:roles package. However, the problem is that I'm not sure if I set the 'user' role as default.

The picture below shows the code for the createUser function.

Client//Account//account.js

Template.signup.events({
  'submit form': function(event) {
    event.preventDefault();
    var nameVar =  event.target.signupName.value;
    var emailVar = event.target.signupEmail.value;
    var passwordVar = event.target.signupPassword.value;

    Accounts.createUser({
      name: nameVar,
      email: emailVar,
      password: passwordVar,
      profile: {
        roles: ["user"]
      } 

     });
  }
});

And below code is the Accounts.onLogin function:

Client//Lib//routes.js

if (Meteor.user().roles = 'admin'){
  FlowRouter.go('dashboard');
}

else if (Meteor.user().roles = 'user'){
    FlowRouter.go('account');
}

I hope that you understand what my problem is and I looking forward the solution. In conclusion, I need to have 'admin' and 'user' role, and when it is admin it should go to /admin-dashboard route, if it is user it should go to /account route.

Thank you all :D


Solution

  • Problem is here:

    if (Meteor.user().roles = 'admin'){ // assigning, not equality check
      FlowRouter.go('dashboard');
    }
    
    else if (Meteor.user().roles = 'user'){
        FlowRouter.go('account');
    }
    

    But roles field is an array, so do this instead:

    if (Meteor.user().roles.indexOf('admin') !== -1){
      FlowRouter.go('dashboard');
    }
    
    else if (Meteor.user().roles.indexOf('user') !== -1){
        FlowRouter.go('account');
    }