簡體   English   中英

灰燼簡單身份驗證ApplicationRoute模型功能

[英]Ember-simple-auth ApplicationRoute model function

我有Ember-simple-auth的經典設置,在我使用的ApplicationRoute中

model: function () {
  return Ember.RSVP.hash({
    user: this.store.find('gsUser').then(function(data) {
      return data.get('content')[0]
    })
  });
},

setupController: function(controller, model) {
  this.controllerFor('user').set('content', model.user);
}

當用戶失去授權時,您將打開頁面。 首先觸發ApplicationRoute :: model,服務器返回401,其他執行停止。

GET http://localhost:8000/app_dev.php/api/1/users.json 401 (Unauthorized)
Error while loading route: undefined 

僅在身份驗證成功時才觸發model

我看到有sessionAuthenticationSucceeded但是我已經嘗試了所有方法來收聽它,沒有人工作。 成功驗證用戶身份后,如何偵聽此事件並從服務器獲取數據?

11/06 22:57更新: enter code here

我設法解決的這個問題的一種解決方案, 但似乎完全不是余燼方式

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  skipModelLoading: false,

  beforeModel: function() {
    this.set('skipModelLoading', !this.get('session').get('isAuthenticated'));
  },

  model: function () {
    if (this.get('skipModelLoading')) {
      return;
    }

    return Ember.RSVP.hash({
      user: this.store.find('gsUser').then(function(data) {
        return data.get('content')[0]
      })
    });
  },

  setupController: function(controller, model) {
    if (this.get('skipModelLoading')) {
      return;
    }

    this.controllerFor('user').set('content', model.user);
  }
});

我假設您正在使用該model方法加載經過身份驗證的用戶。 我將以不同的方式進行操作,並將該屬性附加到會話,如以下示例所示: https : //github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html#L101

我認為我為我的問題找到了更多解決方案:

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, {
  onSessionIsAuthenticated: function () {
    var isAuthenticated = this.get('session').get('isAuthenticated');

    if (!isAuthenticated) {
      return false;
    }

    var userController = this.controllerFor('user');

    return Ember.RSVP.hash({
      user: this.store.find('gsUser').then(function (data) {
        userController.set('content', data.get('content')[0]);
      })
    });
  }.observes('session.isAuthenticated').on('init')
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM