简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM