-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: token doesn't refesh after expires (#77)
- Loading branch information
1 parent
012964b
commit 57ba141
Showing
24 changed files
with
267 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
part of 'auth_repository.dart'; | ||
|
||
class AuthRefreshTokenInterceptor extends HttpInterceptor { | ||
final AuthRepository _authRepository = locator.get(); | ||
|
||
@override | ||
final identifier = "auth_refresh_token"; | ||
|
||
@override | ||
Future<void> onException( | ||
HttpException exception, HttpExceptionInterceptorHandler handler) async { | ||
final apiException = ApiException.fromHttpException(exception); | ||
if (apiException == null || apiException != ApiException.invalidToken) { | ||
return handler.next(exception); | ||
} | ||
|
||
try { | ||
await _authRepository.refreshToken(); | ||
} on Exception { | ||
return handler.next(exception); | ||
} | ||
|
||
return handler.retry(exception); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
lib/services/api/auth/params/auth_refresh_token_params.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
part of '../auth_api_service.dart'; | ||
|
||
class AuthRefreshTokenParams extends ApiParams { | ||
factory AuthRefreshTokenParams({ | ||
required String refreshToken, | ||
String? clientId, | ||
String? clientSecret, | ||
}) { | ||
return AuthRefreshTokenParams._( | ||
refreshToken: refreshToken, | ||
clientId: clientId ?? Configs.app.api.clientId, | ||
clientSecret: clientSecret ?? Configs.app.api.clientSecret, | ||
); | ||
} | ||
|
||
AuthRefreshTokenParams._({ | ||
required this.refreshToken, | ||
required this.clientId, | ||
required this.clientSecret, | ||
}); | ||
|
||
String refreshToken; | ||
String clientId; | ||
String clientSecret; | ||
String grantType = "refresh_token"; | ||
|
||
@override | ||
void mapping(Mapper map) { | ||
map<String>( | ||
"refresh_token", refreshToken, (v) => refreshToken = v as String); | ||
map<String>("client_id", clientId, (v) => clientId = v as String); | ||
map<String>( | ||
"client_secret", clientSecret, (v) => clientSecret = v as String); | ||
map<String>("grant_type", grantType, (v) => grantType = v as String); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
part of 'http_service.dart'; | ||
|
||
abstract class HttpInterceptor { | ||
String get identifier; | ||
|
||
void onException( | ||
HttpException exception, | ||
HttpExceptionInterceptorHandler handler, | ||
) => | ||
handler.next(exception); | ||
|
||
Interceptor toInterceptor(Dio dio) { | ||
return InterceptorsWrapper(onError: (e, handler) { | ||
onException( | ||
HttpException.fromDioError(e), | ||
HttpExceptionInterceptorHandler._(dio: dio, handler: handler), | ||
); | ||
}); | ||
} | ||
} | ||
|
||
class HttpExceptionInterceptorHandler { | ||
const HttpExceptionInterceptorHandler._({ | ||
required Dio dio, | ||
required ErrorInterceptorHandler handler, | ||
}) : _dio = dio, | ||
_handler = handler; | ||
|
||
final ErrorInterceptorHandler _handler; | ||
final Dio _dio; | ||
|
||
void next(HttpException exception) { | ||
_handler.next(exception.error as DioError); | ||
} | ||
|
||
void reject(HttpException exception) { | ||
_handler.reject(exception.error as DioError); | ||
} | ||
|
||
Future<void> retry(HttpException exception) { | ||
final requestOptions = (exception.error as DioError).requestOptions; | ||
|
||
return _dio.request(requestOptions.path, | ||
cancelToken: requestOptions.cancelToken, | ||
data: requestOptions.data, | ||
onReceiveProgress: requestOptions.onReceiveProgress, | ||
onSendProgress: requestOptions.onSendProgress, | ||
queryParameters: requestOptions.queryParameters, | ||
options: requestOptions as Options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.