I was working with the Http
clase from Angular but I decide to make the migration and work with the new HttpClient
, and I was trying to create a solution with Interceptors
to manage the cases when I need to refresh the token and when I need to modify the header to put the Authorization Token.
相关问题
- Angular RxJS mergeMap types
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- How to update placeholder text in ng2-smart-table?
- Google Apps Script: testing doPost() with cURL
- How to instantiate Http service in main.ts manuall
相关文章
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- angular脚手架在ie9+下兼容问题
- angular 前端项目 build 报错 "Cannot find module 'le
- Angular Material Stepper causes mat-formfield to v
- After upgrade to Angular 9 cannot find variable in
- is there any difference between import { Observabl
- Suppress “Circular dependency detected” suppress w
- How can you get current positional information abo
First I found these post and so many others :
... but those solutions are perfect if you just want to handle the action to put the Authorization Header. Then I come up with this solution
The main idea with this is simple:
HttpClient
in theOAuthService
so It's gonna pass from the interceptor too and It's gonna make a infinite loop if I don't check it.After this I found this solution but I wanna know what do you think about code and way that I'm doing it.
Ok, First I created a Service to save the state of the refresh token request and Observable to know when the request is done.
This is my Service:
This the Interceptor for Refresh the Token:
So here I'm waiting to the refresh token to be available or fails and then I release the request that needs the Authorization Header.
This is the Interceptor to put the Authorization Header:
And my main module is something like this:
Please any feedback will be welcome and if I'm doning something wrong tell me. I'm testing with Angular 4.4.6 but I don't know if it work on angular 5, I think should work.
For anyone else looking for a solution to this in Angular 4 (maybe slight changes needed for Angular 5+), I came up with the following solution:
This will issue an auth token refresh request to the server if the current auth token is expired, but there is a valid refresh token. Further requests are buffered until the pending refresh request completes.
Not shown is the source to:
-
TokenStorageService
which just uses localStorage-
Jwt
class that wraps a token and makes the token claims like expiry date easy to access-
ApiResult
which is just a simple wrapper aroundHttpResponse
for my application, and not particularly relevant to anything hereEdit: Angular 6/7