Redirect after Login using Meteor and Iron Router

user2243825 picture user2243825 · Jan 23, 2014 · Viewed 22.2k times · Source

I'm using the built in loginButtons options with Meteor and I would like to redirect after a user logs in. Using the built in web snippets means I can't use the callback with Meteor.loginwithPassword and I can't see any hooks inside Iron-Router to do the redirect.

Any suggestions?

Answer

Charlie Morris picture Charlie Morris · May 6, 2014

Meteor often renders so quickly that the page is being loaded before the user has been defined. You need to use Meteor.loggingIn() to account for the situation in which you are in the process of logging in. This code works for me:

this.route('myAccount', {
  path: '/',
  onBeforeAction: function () {
    if (! Meteor.user()) {
      if (!Meteor.loggingIn()) Router.go('login');
    }
  }
}