我的应用访问 token 每 10 分钟就会过期。我使用 ember simple auth 进行身份验证,但它是一个自定义身份验证器,因为我不仅仅是用户名和密码字段。我想使用 ember-simple-auth-token,但是在我的适配器中调用授权:jwt,它不起作用,显然是因为我没有使用它的身份验证器。
因此,我正在考虑使用每十分钟更新一次 token 的服务,例如 http://www.davekerr.co/programming/2015/05/17/add-a-polling-service-to-your-ember-app/ ,所以我的问题是如何覆盖 sessionData token 。
我的自定义授权者文件代码如下所示:
import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';
export default Base.extend({
authorize(sessionData, block) {
if (!Ember.isEmpty(sessionData.token)) {
block('Authorization', 'Bearer '+sessionData.token);
block('Content-Type', 'application/json');
}
}
});
最佳答案
您应该在自定义身份验证器中执行此操作,因为身份验证器通常负责获取和管理 token 和身份验证状态。您可以简单地安排一个计时器,在初始身份验证后的某个时间更新 token 。一旦 token 被刷新,验证器就可以触发 sessionDataUpdated
event session 将自动处理。
检查implementation of the OAuth2PasswortGrantAuthenticator有关如何从身份验证器更新 token 的示例。
关于authentication - 刷新访问 token ember,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705406/