Skip to content
Discussion options

You must be logged in to vote

A robust way to solve this problem is to combine a refresh lock mechanism with request queuing to ensure that only one refresh request is executed at a time.

The typical pattern works like this:

  1. Intercept responses using a Dio interceptor.
  2. When a request fails with a 401 status code, check if a refresh process is already running.
  3. If a refresh is already running, store the pending request and wait for the refresh to complete.
  4. If no refresh is running, start the refresh token request.
  5. Once the new token is obtained, update the Authorization header and retry all pending requests.

A simplified example structure could look like this:

class AuthInterceptor extends Interceptor {
  final Dio di…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by aiotronic-pe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Programming Help Discussions around programming languages, open source and software development Welcome 🎉 Used to greet and highlight first-time discussion participants. Welcome to the community!
3 participants