2017-10-14 33 views
1

如果用戶從我的API中收到403,我想將用戶重定向到/login。以下是我在我的/app/routes/application.js在Ember.js中處理403

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,引發異常,而不是由路由器處理的,任何提示?

回答

1

您可以覆蓋您的Ember Data應用程序適配器來捕獲403s。做那裏的事情,然後讓你處理的事情,以觸發重定向所需:

// 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); 
    } 
}); 
+0

你能告訴我一個這樣的例子嗎?我不確定我明白。 –

+0

當然,只是更新。這應該能處理你之後的事情。 – acorncom