Iron Router, best practice

Pls what better way could I write this piece of code, I have read the documentation but I don’t understand it or can’t find a specific example that reflect what I want to do.

Router.route('/', function () {
  this.render('Home');
});

Router.route('/admin', function () {
    if (!Meteor.userId()) {
      this.render('loginpage');
    }
    this.next();
    if (Meteor.user().profile.role=="admin"){
       this.render('administrator');
    }
    this.next();
    if (Meteor.user().profile.role=="assessor"){
       this.redirect('/assessor');
    }
});

Router.route('/assessor', function () {
    if (!Meteor.userId()) {
      this.render('loginpage');
    }
    this.next();
    if (Meteor.user().profile.role=="assessor"){
       this.render('assessor');
    }
    this.next();
    if (Meteor.user().profile.role=="admin"){
       this.redirect('/admin');
    }
});

While this was a way of restricting access to routes/content before, I think most people would suggest that you handle this at the template level.

I would add that if you are just starting out with Meteor, iron:router is not the best choice. You should look at flow-router or react-router for client-side routing.

If you also want server-side routing, look at simple:rest, nimble:restivus, centiq:crud or express.

1 Like

I’m new too and started to work in a project already running and didn’t have much choice about the kind of router so… do you recommend something to read that can help more than the docummentation?

I’ve never used iron router, but there’s this: http://meteortips.com/second-meteor-tutorial/iron-router-part-1/

this: http://meteortips.com/second-meteor-tutorial/iron-router-part-2/

and this: http://meteortips.com/second-meteor-tutorial/iron-router-part-3/

1 Like

Thank you very much. I wish i had seen this earlier .

Was a very good intro =)

better late than never, right.

1 Like