22

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?

user2243825
  • 255
  • 1
  • 2
  • 6
  • did you try this https://github.com/EventedMind/iron-router#using-hooks Router.before(mustBeSignedIn, {except: ['login', 'signup', 'forgotPassword']}); – pahan Jan 23 '14 at 18:09
  • I can't see any example pieces of code using this to see how to use it – user2243825 Jan 23 '14 at 19:20

4 Answers4

22

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');
    }
  }
}
Minderov
  • 511
  • 1
  • 7
  • 20
Charlie Morris
  • 476
  • 7
  • 14
6

This example might be useful

// main route render a template
Router.route('/', function () {
    this.render('main');
});

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


// we want to be sure that the user is logging in
// for all routes but login
Router.onBeforeAction(function () {
    if (!Meteor.user() && !Meteor.loggingIn()) {
        this.redirect('/login');
    } else {
        // required by Iron to process the route handler
        this.next();
    }
}, {
    except: ['login']
});

// add here other routes

// catchall route
Router.route('/(.*)', function () {
    this.redirect('/catchallpage');
});
Brice
  • 666
  • 5
  • 10
5

it should be very easy just add something like:

Tracker.autorun(function() {
  var currentRoute = Router.current();
  if (currentRoute === null) {
    return;
  }

  if (currentRoute.route.getName() === 'login' && Meteor.user() !== null)
    Router.go('WelcomeNewUser');
  }

You can also just use the same route with another template in case the user is not logged in.

just something like this:

this.route('myAccount', {
   before: function () {
     if (!Meteor.user()) {
       this.render('login');
       this.stop();
     }
   }
}

There is no magic, just looked into the docs ;)

helmbert
  • 28,771
  • 10
  • 73
  • 74
Boris Kotov
  • 1,662
  • 1
  • 12
  • 14
1

You can simply use one of your existing routes you have configured in Ireland route

Router.go('/myRouterPathToTemplate')

johntday
  • 130
  • 3