Using Router on Login

I use AccountsTemplates.

I try to redirect the user to another page on login. But when I call Router.go in my sumbit hook, Router is undefined. I tried to change the loading order by changing the filename of my accountConfig but it didnot work.

AccountsTemplates.configure(
onSubmitHook:
Router.go(’/map’) # HERE ROUTER IS STILL UNDEFIED
)

1 Like

Depending on a few different variables you can run your Router.go("/map") after a successful login. For example:

Meteor.loginWithPassword(user, password, function(err){
  if(err) {
    // do error handling
  } else {
    Router.go("/map");
  }
});

The same logic can be used with external services assuming that you don’t have the the loginStyle configured to "redirect".

But then I would have to write my own accounts code, right? (and cannot use AccountsTemplates anymore)

You can use Router.onBeforeAction. However in this case I have only a single page where the user can log in, as it’s essentially a single page app with a logged out page and a couple logged in pages you can’t visit while anonymous.

For example:

redirectOnLogin = function(){
  if (Meteor.userId()) {
    this.render('dashboard');
  }else{
    this.next();
  }
}
Router.onBeforeAction(redirectOnLogin, {only: ['loginPage']})

If you hit the login page ('/') while logged in you’ll get redirected, or more likely you’re on the login page then you log in.

Hey @obiwahn!
Author of AccountsTemplates here…

Lets follow the docs about Rooting.

For your use case it should be something like:

AccountsTemplates.configureRoute('signIn', {
    redirect: '/map',
});

In case you’re not using routes configuration, you can try:

var mySubmitFunc = function(error, state){
  if (!error) {
    if (state === "signIn") {
      // Successfully logged in
      Meteor.defer(function(){
        Router.go('/map');
      });
    }
    if (state === "signUp") {
      // Successfully registered
      // ...
    }
  }
};

AccountsTemplates.configure({
    onSubmitHook: mySubmitFunc
});

beware that Router.go might want a route name and not a path, not sure about this right now…

Accounts.onLogin(function () {
  Router.go('/map');
});

Works no matter how you log in.

Source:
http://docs.meteor.com/#/full/accounts_onlogin