简体   繁体   English

在触发请求之前刷新Interceptor中的令牌

[英]Refresh Token in Interceptor before request is fired

Pulling my hair out on this one. 把头发拉出来。 I would like to refresh an access token if the user's access token is just about to expire. 如果用户的访问令牌即将到期,我想刷新访问令牌。

authService.isUserLoggedIn() returns a promise and checks if the the user is logged in or not. authService.isUserLoggedIn()返回一个promise并检查用户是否已登录。 If not the user's access token is being refreshed. 如果不是,则刷新用户的访问令牌。

However the problem is that authService.isUserLoggedIn() is async call and before it returns the value, the interceptor will finish its job and the Authorization header will not be populated with the new token... 但问题是authService.isUserLoggedIn()是异步调用,在它返回值之前,拦截器将完成其作业,并且不会使用新标记填充Authorization标头...

I was looking for a way to wait for the promise to resolve before the script continues. 在脚本继续之前,我一直在寻找一种等待承诺解决的方法。 Unfortunately I'm not able to complete what is required. 不幸的是,我无法完成所需的任务。

Code: 码:

.factory('SEHttpInterceptor', function($injector, ngWebApiSettings) {
    return {
        // optional method
        'request': function(config) {

          // add Authorization header if available
          if (config.url.indexOf(ngWebApiSettings.apiServiceBaseUri) >-1){
            var authService = $injector.get('authService2');
              authService.isUserLoggedIn().then(function(response){
                var authData = $injector.get('$localStorage').getObject("authorizationData");
                config.headers.Authorization = 'Bearer ' + authData.token;
              });
          }   
          return config;
        }
      };
});

From AngularJS $http documentation : 来自AngularJS $ http文档

The interceptors leverage the promise APIs to fulfill this need for both synchronous and asynchronous pre-processing. 拦截器利用promise API来满足同步和异步预处理的这种需求。

request : interceptors get called with a http config object. request :使用http配置对象调用拦截器。 The function is free to modify the config object or create a new one. 该函数可以自由修改配置对象或创建新对象。 The function needs to return the config object directly, or a promise containing the config or a new config object. 该函数需要直接返回配置对象,或者包含配置或新配置对象的promise。

I'm assuming that you can simply: 我假设你可以简单地说:

'request': function(config) {

    if (config.url.indexOf(ngWebApiSettings.apiServiceBaseUri) === -1){
        return config;
    }

    var authService = $injector.get('authService2');
    return authService.isUserLoggedIn().then(function(response){
        var authData = $injector.get('$localStorage').getObject("authorizationData");
        config.headers.Authorization = 'Bearer ' + authData.token;
        return config;
    });

}

Thanks to Oleg's suggestion, I have managed to get this working. 感谢Oleg的建议,我设法让这个工作。 The answer was to return a promise, within which we return the config key. 答案是返回一个promise,我们在其中返回config密钥。

.factory('SEHttpInterceptor', function($injector, ngWebApiSettings) {
     return {
        // optional method
        'request': function(config) {

          // add Authorization header if available
          if (config.url.indexOf('/Token')== -1 && config.url.indexOf(ngWebApiSettings.apiServiceBaseUri) >-1){
            var authService = $injector.get('authService2');
              return authService.isUserLoggedIn().then(function(response){
                var authData = $injector.get('$localStorage').getObject("authorizationData");
                config.headers.Authorization = 'Bearer ' + authData.token;
                return config;
              });
          }   
          return config;
        }
      };
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM