【问题标题】:How to invoke index/index route's model hook after user's authorization?用户授权后如何调用索引/索引路由的模型钩子?
【发布时间】:2016-04-07 19:21:38
【问题描述】:

您好,我正在使用 Ember 和 Firebase 制作一个应用程序,用户可以在其中创建一个新帐户、登录,并且只有在他获得授权时才能看到有关他的帐户和一些受保护路由的信息。这样非授权用户只能看到 2 条路由:显示登录表单的索引 ('/') 和显示并允许他们创建新帐户的注册路由 ('/signup') 的链接。我的 app/router.js 路由器看起来像这样:

Router.map(function() {
  this.route('signup');
  this.route('index', {path: '/'}, function() {
    this.authenticatedRoute('index', {path: '/'});
    this.authenticatedRoute('events');
    this.authenticatedRoute('about');
  });
});

app/templates/index.hbs 包含登录表单,当用户登录嵌套索引路由时会呈现,但它的模型挂钩在刷新之前不会被调用。如何让它发挥作用?

app/templates/index.hbs

{{#unless session.isAuthenticated}}

  {{signin-form store=store session=session authorized="authorized"}}

{{else}}

  {{#link-to 'index.index'  class="item"}}Home{{/link-to}}
  {{#link-to 'index.events' class="item"}}Events{{/link-to}}
  {{#link-to 'index.about'  class="item"}}About{{/link-to}}


  {{outlet}}

{{/unless}}

app/routes/application.js

import Ember from 'ember';

export default Ember.Route.extend({
  beforeModel() {
    return this.get("session").fetch().catch(function() {});
  },
  actions: {
    signOut() {
      this.get('session').close();
      this.transitionTo('index');
    },
    accessDenied() {
      this.transitionTo('index');
    },
    authorized() {
      console.log('authorized');
      this.transitionTo('index');
    }
  }
});

app/components/signin-form.js

import Ember from 'ember';

export default Ember.Component.extend({
  didRender() {
    this._super(...arguments);
    this.$('form.ui.form').form();
  },

  signIn() {
    let { userPassword, userEmail }
      = this.getProperties('userPassword', 'userEmail');
    console.log('Logging with', userPassword, userEmail);
    this.get("session").open("firebase", { 
        provider: 'password',
        email: userEmail,
        password: userPassword
    }).then((data) => {
      console.log(data);
      this.sendAction('authorized');
    });
  },

  actions: {
    submit() {
      this.signIn();
    }
  }

});

【问题讨论】:

    标签: javascript ember.js firebase ember-data emberfire


    【解决方案1】:

    在 app/routes/application.js 我已经改变了

    authorized() {
      this.transitionTo('index');
    }
    

    进入

    authorized() {
      this.refresh();
    }
    

    现在它可以工作了。但总的来说,我仍然对这个解决方案有疑问,所以如果你看到更好/更棒的方法,我会很高兴听到它。

    【讨论】:

    • 我建议使用单独的登录路径,然后使用 beforeModel 挂钩将用户重定向到该路径。看看ember-simple-auth 是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    相关资源
    最近更新 更多