From f257cbde3a4f991372fd8213a19f3ad55b02c8e7 Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 17 Jul 2023 12:41:00 +0700 Subject: [PATCH 1/2] Add condition when token expired --- .../config/authorization_interceptors.dart | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/features/login/data/network/config/authorization_interceptors.dart b/lib/features/login/data/network/config/authorization_interceptors.dart index 702d2fd2bd..2c62537e3b 100644 --- a/lib/features/login/data/network/config/authorization_interceptors.dart +++ b/lib/features/login/data/network/config/authorization_interceptors.dart @@ -75,32 +75,36 @@ class AuthorizationInterceptors extends InterceptorsWrapper { @override void onError(DioError err, ErrorInterceptorHandler handler) async { - final requestOptions = err.requestOptions; log('AuthorizationInterceptors::onError(): $err'); - if (_isTokenExpired() && - err.response?.statusCode == 401 && + try { + if (_isTokenExpired() && + (err.response == null || err.response?.statusCode == 401) && _isRefreshTokenNotEmpty() && - _isAuthenticationOidcValid()) { - try { + _isAuthenticationOidcValid() + ) { final newToken = await _authenticationClient.refreshingTokensOIDC( - _configOIDC!.clientId, - _configOIDC!.redirectUrl, - _configOIDC!.discoveryUrl, - _configOIDC!.scopes, - _token!.refreshToken); + _configOIDC!.clientId, + _configOIDC!.redirectUrl, + _configOIDC!.discoveryUrl, + _configOIDC!.scopes, + _token!.refreshToken); final accountCurrent = await _accountCacheManager.getSelectedAccount(); + await _accountCacheManager.deleteSelectedAccount(_token!.tokenIdHash); + await Future.wait([ _tokenOidcCacheManager.persistOneTokenOidc(newToken), - _accountCacheManager.deleteSelectedAccount(_token!.tokenIdHash), - _accountCacheManager.setSelectedAccount(PersonalAccount( - newToken.tokenIdHash, - AuthenticationType.oidc, - isSelected: true, - accountId: accountCurrent.accountId, - apiUrl: accountCurrent.apiUrl, - userName: accountCurrent.userName)) + _accountCacheManager.setSelectedAccount( + PersonalAccount( + newToken.tokenIdHash, + AuthenticationType.oidc, + isSelected: true, + accountId: accountCurrent.accountId, + apiUrl: accountCurrent.apiUrl, + userName: accountCurrent.userName + ) + ) ]); log('AuthorizationInterceptors::onError(): refreshToken: $newToken'); @@ -108,16 +112,16 @@ class AuthorizationInterceptors extends InterceptorsWrapper { _updateNewToken(newToken.toToken()); - requestOptions.headers[HttpHeaders.authorizationHeader] = - _getTokenAsBearerHeader(newToken.token); + final requestOptions = err.requestOptions; + requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(newToken.token); final response = await _dio.fetch(requestOptions); return handler.resolve(response); - } catch(e) { - log('AuthorizationInterceptors::onError(): $e'); + } else { super.onError(err, handler); } - } else { + } catch (e) { + log('AuthorizationInterceptors::onError():Exception: $e'); super.onError(err, handler); } } From 54f3cd862d2071d27300b3aa2ec964a645b598ed Mon Sep 17 00:00:00 2001 From: dab246 Date: Mon, 17 Jul 2023 12:41:26 +0700 Subject: [PATCH 2/2] Add `offline_access` to oidc scopes --- env.file | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/env.file b/env.file index 989479a995..273e74325b 100644 --- a/env.file +++ b/env.file @@ -1,7 +1,7 @@ SERVER_URL=http://localhost/ DOMAIN_REDIRECT_URL=http://localhost:3000 WEB_OIDC_CLIENT_ID=teammail-web -OIDC_SCOPES=openid,profile,email +OIDC_SCOPES=openid,profile,email,offline_access APP_GRID_AVAILABLE=supported FCM_AVAILABLE=supported IOS_FCM=supported \ No newline at end of file