Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a code for an auth interceptor for Dio, which uses a header to avoid retrying indefinitely to refresh the token.
/// This interceptor is simplified because is doesn't contemplate
/// a refresh token, which you should take care of.
/// I haven't tested this code, but I believe it should work.
/// If you have some feedback, please leave a comment and I'll review it :) Thanks.
class AuthInterceptor extends InterceptorsWrapper {
final Dio loggedDio;
final Dio tokenDio;
final SomeLocalStorage localStorage;
AuthInterceptor({this.loggedDio, this.tokenDio});
@override
Future onRequest(RequestOptions options) async {
/// access token obtained via API after user logged in and saved into
/// some local storage
final accessToken = localStorage.getAccessToken();
options.headers['Authorization'] = accessToken;
return options;
}
@override
Future onError(DioError error) async {
if (e.response?.statusCode == 401) {
//This retries the request if the token was updated later on
RequestOptions options = error.response.request;
if (options.headers['Authorization'] != localStorage.getAccessToken()) {
return loggedDio.request(options.path, options: options);
} else {
_lockDio();
if (options.headers['Retry-Count'] == 1) {
_unlockDio();
// Here you might want to logout the user
return error;
}
return tokenDio.get("/token").then((response) {
localStorage.setAccessToken(response.data['data']['token']);
}).whenComplete(() {
_unlockDio();
}).then((e) {
options.headers['Retry-Count'] = 1;
return loggedDio.request(options.path, options: options);
}).catchError((e) {
// Here you might want to logout the user
return e;
});
}
} else {
return error;
}
}
void _retryRequest() {
}
void _lockDio() {
loggedDio.lock();
loggedDio.interceptors.responseLock.lock();
loggedDio.interceptors.errorLock.lock();
}
void _unlockDio() {
loggedDio.unlock();
loggedDio.interceptors.responseLock.unlock();
loggedDio.interceptors.errorLock.unlock();
}
}
@iamnabink

This comment has been minimized.

Copy link

@iamnabink iamnabink commented Mar 16, 2021

Worked like a charm, Thanks man

@minhbnt-1512

This comment has been minimized.

Copy link

@minhbnt-1512 minhbnt-1512 commented May 14, 2021

please support dio version 4.0.0

@douglasiacovelli

This comment has been minimized.

Copy link
Owner Author

@douglasiacovelli douglasiacovelli commented May 14, 2021

Hey, I haven't been able to migrate the project I work on to flutter 2 and dart 2.12, but I'll have a try over the weekend to see what happens. It shouldn't be too different if the Dio API follows the same pattern as before, but I'll check it.

@omarhuss

This comment has been minimized.

Copy link

@omarhuss omarhuss commented Jun 2, 2021

Hey, I haven't been able to migrate the project I work on to flutter 2 and dart 2.12, but I'll have a try over the weekend to see what happens. It shouldn't be too different if the Dio API follows the same pattern as before, but I'll check it.

@douglasiacovelli Any updates please?

@douglasiacovelli

This comment has been minimized.

Copy link
Owner Author

@douglasiacovelli douglasiacovelli commented Jun 2, 2021

Not yet! I'll be checking this this week and return if I have any news :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment