Search code examples
flutterinterceptorrefresh-token

JWT / RefreshToken in Flutter with ExpiredTokenRetryPolicy / Interceptor


I'm trying to use an InterceptorContract to intercept my calls and check whether my refreshtoken has expired.

I would like to handle a 401 by requesting a new token and retrying the previous request, however I can't figure out how to implement this using the ExpiredTokenRetryPolicy/http_interceptor. Does anyone have a suggestion or an example on how to implement this?

I looked at the example in the http_interceptor package but that didn't really make it clear.

ExpiredTokenRetryPolicy


Solution

  • After trying a few things, I found out that I could add the policy in the constructor of the http interceptor: add policy

    and then call the retry policy in my method after getting a 401 call retry policy

    In my shouldAttemptRetryOnResponse I check if it's a 401, and then I call the function to refresh my function.

    Probably not the best way to do this, but it works :)