在 Ember.js 中处理 403

Handling a 403 in Ember.js

如果用户从我的 API 收到 403,我想将他们重定向到 /login。这是我 /app/routes/application.js:

中的内容
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth-auth0/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  actions: {
    error(error, transition) {
      if (error.status === '403') {
        this.replaceWith('login');
      } else {
        return true;
      }
    }
  }
});

但这似乎不能很好地处理 Ember 数据,当返回 403 时,会引发异常,但路由器不会处理,有什么提示吗?

您可以覆盖 Ember 数据应用程序适配器以捕获 403。在那里做事然后允许您处理事情以根据需要触发重定向:

// app/adapters/application.js

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  handleResponse(status, headers, payload, requestData) {
    if(status === 403) {
      // throw an error to be caught at the app level
      // or work with a service to handle things as desired
    }

    return this._super(status, headers, payload, requestData);
  }
});