-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refresh-token-interceptor): implement refresh token interceptor
Signed-off-by: Taranjeet Singh <[email protected]>
- Loading branch information
1 parent
dedd2a7
commit 11b93e8
Showing
6 changed files
with
89 additions
and
14 deletions.
There are no files selected for viewing
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
12 changes: 12 additions & 0 deletions
12
lib/src/implementations/refresh_token_logging_interceptor_impl.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,12 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:fa_flutter_api_client/fa_flutter_api_client.dart'; | ||
|
||
/// This is only for refresh token logging | ||
/// Do not use it for other purposes | ||
class RefreshTokenLoggingInterceptorImpl extends LoggingInterceptor { | ||
@override | ||
void logPrint(String msg) { | ||
log(msg); | ||
} | ||
} |
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,29 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:dio/dio.dart'; | ||
|
||
abstract class RefreshTokenInterceptor extends QueuedInterceptorsWrapper { | ||
bool isRefreshingSession = false; | ||
|
||
@override | ||
FutureOr<void> onRequest( | ||
RequestOptions options, | ||
RequestInterceptorHandler handler, | ||
) async { | ||
try { | ||
var updatedOptions = options; | ||
if (isExpired(options)) { | ||
updatedOptions = await refreshToken(options); | ||
} | ||
return handler.next(updatedOptions); | ||
} catch (e) { | ||
handler.next(options); | ||
} | ||
} | ||
|
||
/// It will be called when [isExpired] is true | ||
/// Return the updated [RequestOptions] with new token | ||
FutureOr<RequestOptions> refreshToken(RequestOptions options); | ||
|
||
bool isExpired(RequestOptions options); | ||
} |