如何在使用Ember Auth成功登录时转换为root用户



因此,我使用纯基于令牌的方法成功地实现了Ember Auth。我想在我的用户登录后将其重定向到我的应用程序的根目录。

我知道我可以使用actionRedirectable(http://ember-auth.herokuapp.com/docs在文档中),但由于我使用的是纯令牌方法,并且不在cookie中存储任何内容,因此每次使用remember_token刷新页面时,我都会有效地让我的用户再次登录(这似乎很难,但我很快就会解决)。这意味着使用actionRedireactable意味着每次用户刷新页面时我都会重定向。也许某个地方有反模式?

总之,这是我的SignInView:

App.SignInView = Ember.View.extend({
  templateName: 'auth/sign_in',
  email:    null,
  password: null,
  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();
    App.Auth.signIn({
      data: {
        email:    this.get('email'),
        password: this.get('password')
      }
    });
  }
});

如果我在signIn调用之后直接调用this.get("controller").transitionToRoute('...'),那么我的用户此时总是没有登录,所以他们会再次重定向到登录页面。如果我尝试:

App.Auth.on('signInSuccess', function() {
  // ...
});

那么我没有任何合理的方法来访问路由器来进行转换。任何好主意都将不胜感激。谢谢

作为一种最佳实践,您的视图中不应该有逻辑,逻辑更适合生活在控制器中,因此对于您的用例,请在那里创建一个App.SignInController——一个您的身份验证过程中的工具:

查看

App.SignInView = Ember.View.extend({
  templateName: 'auth/sign_in',
  email:    null,
  password: null,
  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();
    var data = {
        email:    this.get('email'),
        password: this.get('password')
    }
    // forward the action to your controller passing along the
    // data object your sign in process needs
    this.get("controller").send("signIn", data);
  }
});

此外,您不应该从路由器内部以外的其他地方转换To。这样做,你可能会遇到严重的问题,因为你不知道你的路由器实际上在哪个state中。所以最好的办法是获得你的路由器的参考,并在路由器上调用transitionTo

控制器

App.SignInController = Ember.ObjectController.extend({
  signIn: function(data) {
    // grab your passed data object and issues you sign in
    App.Auth.signIn({
      data: data
    });
    
    // subscribe to the `signInSuccess` event and 
    // then transition to your route but using 
    // the router itself
    App.Auth.one('signInSuccess', function() {
      var router = this.get('target.router');
      router.transitionTo('route_name');
    });
  }
});

希望这能有所帮助。

我还没有测试过,但我认为这很有效:

App.SignInView = Ember.View.extend({
  templateName: 'auth/sign_in',
  email:    null,
  password: null,
  submit: function(event, view) {
    event.preventDefault();
    event.stopPropagation();
    var controller = this.get('controller');
    App.Auth.signIn({
      data: {
        email:    this.get('email'),
        password: this.get('password')
      }
    });
    App.Auth.one('signInSuccess', function() {
      controller.transitionToRoute('...');
    });
  }
});

最新更新