Authentication 如何处理简单余烬身份验证中的错误401?
问题是会话将在预定的时间后过期。很多时候,当这种情况发生时,ember.js应用程序仍然被加载。因此,所有对后端的请求都返回401{not autorized}响应 因此,我需要将用户重定向到登录页面并清除会话中的最后一个令牌,以便isauthenticated属性变为false 我正在使用自定义验证器Authentication 如何处理简单余烬身份验证中的错误401?,authentication,ember.js,ember-cli,Authentication,Ember.js,Ember Cli,问题是会话将在预定的时间后过期。很多时候,当这种情况发生时,ember.js应用程序仍然被加载。因此,所有对后端的请求都返回401{not autorized}响应 因此,我需要将用户重定向到登录页面并清除会话中的最后一个令牌,以便isauthenticated属性变为false 我正在使用自定义验证器 import Base from 'ember-simple-auth/authenticators/base'; import ENV from '../config/environment'
import Base from 'ember-simple-auth/authenticators/base';
import ENV from '../config/environment';
import Ember from 'ember';
export default Base.extend({
restore: function(data) {
return new Ember.RSVP.Promise(function (resolve, reject) {
if (!Ember.isEmpty(data.token)) {
resolve(data);
}
else {
reject();
}
});
},
authenticate: function(options) {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
type: "POST",
contentType: 'application/json',
url: ENV.CONSTANTS.API_URL + '/authentication',
data: JSON.stringify({
username: options.username,
password: options.password
})
}).then(function(response) {
if(!response.token){
Ember.run(function(){
reject(response.message);
});
} else {
Ember.run(function() {
resolve(response);
});
}
}, function(xhr, status, error) {
Ember.run(function() {
reject(xhr.responseJSON || xhr.responseText);
});
});
});
},
invalidate: function(data) {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
type: "GET",
url: ENV.CONSTANTS.API_URL + '/authentication/logout'
}).then(function(response) {
Ember.run(function() {
resolve(response);
});
}, function(xhr, status, error) {
Ember.run(function() {
reject(xhr.responseJSON || xhr.responseText);
});
});
});
}
});
我使用的是ember simple auth 1.0.0。有人对此问题有有效的解决方案吗?如果您正在使用,它将自动处理所有401对Ember数据请求的响应,并在会话收到响应时使会话无效。如果您要发出自己的AJAX请求,您必须自己处理这些响应
所有请求的自动授权以及自动响应处理在1.0.0中被删除,因为这会导致全局状态出现许多问题,并使整个库更难推理。操作:{error:function(error,transition){if(error&&error.status==401){返回此。transitiono('index'));}返回true;}},我已经使用这个函数处理了401应用内路由。现在如何使客户端无效?使用来使会话无效。但它不会调用自定义验证器的invalidate属性吗?在我的例子中,它将尝试发出一个ajax注销请求,并再次返回401。是的,它将调用验证器的
invalidate
方法。我想验证器应该处理失效失败的事实,因为会话已经无效。