Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 749
mfilimonov Avatar asked Nov 10 '22 01:11

mfilimonov


1 Answers

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.

like image 200
saimeunt Avatar answered Nov 27 '22 01:11

saimeunt