Search code examples
meteoriron-router

Accounts.onLogin with IronRouter


I'm trying to redirect user to a page after login. Trying to use Router.go from Accounts.onLogin callback:

Accounts.onLogin () ->
  Router.go('users.new')

When I try this on the server I get TypeError: Object [object Object] has no method 'go'

On the client I get Accounts.onLogin undefined


Solution

  • Well, you've got a problem here :

    Accounts.onLogin is undefined on the client because it is a server-only API.

    UPDATE 15/06/2015 : this is no longer true, Accounts.onLogin is now available on the client too.

    Router.go is undefined on the server because redirecting with iron:router is a client-only API.

    If you are using {{> loginButtons}} you can try this workaround on the client :

    Tracker.autorun(function(){
      if(Meteor.user()){
        // login handler
        Router.go("users.new");
      }
      else{
        // logout handler
      }
    });
    

    If you are using a custom login form with Meteor.loginWithSomething, you can perform the redirection in the success callback of the login method.