voliva / angular2-interceptors

79 stars 20 forks source link

Add an example for retry #15

Open thekalinga opened 7 years ago

thekalinga commented 7 years ago

Can u add an example to the readme on how to do retry. For eg., in OAuth2, when a request is made, if the auth token has expired, the client (browser) needs to refresh auth token using refresh token & make a new request for the previous failed call using this new auth token

If for some reason, both auth & refresh tokens are expired, the user needs to be sent to login page.

Can you update the documentation to reflects flows of this nature

thekalinga commented 7 years ago

Here is the complete OAuth2 flow

OAuth2 refresh token flow

voliva commented 7 years ago

Sure, I'll try to put an example to describe this case, as it's quite complex.

I'll think more about it when I have more time, but I think the way to go is that all this flow is managed by an interceptor, call him "AuthInterceptor". Then in interceptAfter (where we get the response from the server) we can check for OAuth token expired. If it has, then we can send another request from the same interceptor to renew the token, because we can actually return a Observable<InterceptedResponse> on interceptAfter, allowing us to do async stuff inside.

I still have to create some usage examples, and this one looks great to start with.