Iron Rout noob question

Hi guys,

I am trying to create a very simple login/sign up system using Iron Route. When user logs in, I would like do direct him to my logged in template:

<template name="cards">
  <h2>Cards</h2>
   {{#if currentUser}}
      Logged in
      <input type="button" class="logout" value="Logout">
   {{/if}}
</template>

Simple stuff.

My events template is:

Template.login.events({
  'click .register' (event, template) {
      Router.go('signup');
  },
  'submit form' (event, template) {
    event.preventDefault();
    var emailVar = $("#email").val();
    var passwordVar = $("#password").val();
    Meteor.loginWithPassword(emailVar, passwordVar, function(err) {
        if (err) {
          console.log(err);
          Session.set('loginErrorMessage',err.reason);
          Session.set("showError", true);
        } else {
          Session.set("showError", false);
          console.log("success")
          Route.go('cards');
        }
    });
  }
});

The failure is in “Route.go(cards)”. The error is:

Exception in delivering result of invoking ‘login’: ReferenceError: Route is not defined

What could the problem be? Is it the fact that I cannot call Route.go inside of a callback function? How can I fix that?

Thanks all

Yes! I got it. The issue was not using the Iron Route hooks. They are the ones that make this happen. So now I have this:

Router.route('/cards', {
  template: 'cards',
  onBeforeAction: function(){
        var currentUser = Meteor.userId();
        if(currentUser){
            this.next();
        } else {
          this.render('login');
        }
    }
});

Router.route('/', {
    template: 'login',
    name: 'login',
    onBeforeAction: function(){
          var currentUser = Meteor.userId();
          if(currentUser){
              this.render('cards');
          } else {
            this.next();
          }
      }
});

works like a charm.

Question to you guys… Flow Route or Iron Route… should I go for any in specific or both great?

It’s advisable to start using flow router as iron router has been abandoned as far as I know.

1 Like

Definitely FlowRouter.

yeah if you’re just getting started now (“noob question”) you should skip IronRouter. It’s considered legacy. Use FlowRouter (or React Router if you’re using React).

Thanks for the tips guys. Flow route it is theb

1 Like

@fcaldas: Where did you see/read about iron router?

I am curious to know how/why new projects are being started with this package…

I took it from here: http://meteortips.com/second-meteor-tutorial/iron-router-part-1/
It was well written and explained, so went for it.

Thanks, good to know!