Add condition when token expired

(cherry picked from commit 1664c16a1680b60c4fa98f729cebc0e786da0935)
This commit is contained in:
dab246
2023-07-17 12:41:00 +07:00
committed by Dat Vu
parent 37334d3c66
commit 97d9f8fc80
@@ -75,32 +75,36 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
@override @override
void onError(DioError err, ErrorInterceptorHandler handler) async { void onError(DioError err, ErrorInterceptorHandler handler) async {
final requestOptions = err.requestOptions;
log('AuthorizationInterceptors::onError(): $err'); log('AuthorizationInterceptors::onError(): $err');
if (_isTokenExpired() && try {
err.response?.statusCode == 401 && if (_isTokenExpired() &&
(err.response == null || err.response?.statusCode == 401) &&
_isRefreshTokenNotEmpty() && _isRefreshTokenNotEmpty() &&
_isAuthenticationOidcValid()) { _isAuthenticationOidcValid()
try { ) {
final newToken = await _authenticationClient.refreshingTokensOIDC( final newToken = await _authenticationClient.refreshingTokensOIDC(
_configOIDC!.clientId, _configOIDC!.clientId,
_configOIDC!.redirectUrl, _configOIDC!.redirectUrl,
_configOIDC!.discoveryUrl, _configOIDC!.discoveryUrl,
_configOIDC!.scopes, _configOIDC!.scopes,
_token!.refreshToken); _token!.refreshToken);
final accountCurrent = await _accountCacheManager.getSelectedAccount(); final accountCurrent = await _accountCacheManager.getSelectedAccount();
await _accountCacheManager.deleteSelectedAccount(_token!.tokenIdHash);
await Future.wait([ await Future.wait([
_tokenOidcCacheManager.persistOneTokenOidc(newToken), _tokenOidcCacheManager.persistOneTokenOidc(newToken),
_accountCacheManager.deleteSelectedAccount(_token!.tokenIdHash), _accountCacheManager.setSelectedAccount(
_accountCacheManager.setSelectedAccount(PersonalAccount( PersonalAccount(
newToken.tokenIdHash, newToken.tokenIdHash,
AuthenticationType.oidc, AuthenticationType.oidc,
isSelected: true, isSelected: true,
accountId: accountCurrent.accountId, accountId: accountCurrent.accountId,
apiUrl: accountCurrent.apiUrl, apiUrl: accountCurrent.apiUrl,
userName: accountCurrent.userName)) userName: accountCurrent.userName
)
)
]); ]);
log('AuthorizationInterceptors::onError(): refreshToken: $newToken'); log('AuthorizationInterceptors::onError(): refreshToken: $newToken');
@@ -108,16 +112,16 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
_updateNewToken(newToken.toToken()); _updateNewToken(newToken.toToken());
requestOptions.headers[HttpHeaders.authorizationHeader] = final requestOptions = err.requestOptions;
_getTokenAsBearerHeader(newToken.token); requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(newToken.token);
final response = await _dio.fetch(requestOptions); final response = await _dio.fetch(requestOptions);
return handler.resolve(response); return handler.resolve(response);
} catch(e) { } else {
log('AuthorizationInterceptors::onError(): $e');
super.onError(err, handler); super.onError(err, handler);
} }
} else { } catch (e) {
log('AuthorizationInterceptors::onError():Exception: $e');
super.onError(err, handler); super.onError(err, handler);
} }
} }