|
22 | 22 | import android.os.Handler; |
23 | 23 | import android.util.Log; |
24 | 24 |
|
| 25 | +import com.amazonaws.AmazonClientException; |
25 | 26 | import com.amazonaws.AmazonServiceException; |
26 | 27 | import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationContinuation; |
27 | 28 | import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails; |
@@ -644,18 +645,23 @@ private CognitoUserSession getCachedSession() { |
644 | 645 | return cipSession; |
645 | 646 | } |
646 | 647 |
|
647 | | - // Clear any cached tokens, since none of them are valid. |
648 | | - clearCachedTokens(); |
649 | | - |
650 | 648 | if (cachedTokens.getRefreshToken() != null) { |
651 | 649 | // Use Refresh token to get new tokens |
652 | 650 | try { |
653 | 651 | cipSession = refreshSessionInternal(cachedTokens.getRefreshToken()); |
654 | 652 | cacheTokens(cipSession); |
655 | 653 | return cipSession; |
656 | | - } catch (Exception e) { |
| 654 | + } catch (CognitoNotAuthorizedException e) { |
| 655 | + // Clear any cached tokens, since none of them are valid. |
| 656 | + clearCachedTokens(); |
657 | 657 | // Could not get new tokens from refresh. Should authenticate user. |
658 | | - throw new CognitoNotAuthorizedException("user is not authenticated"); |
| 658 | + throw new CognitoNotAuthorizedException("user is not authenticated",e); |
| 659 | + } catch (AmazonClientException e) { |
| 660 | + // General IO errors - not clearing cached tokens |
| 661 | + throw new AmazonClientException("failed to get new tokens from refresh",e); |
| 662 | + } catch (Exception e) { |
| 663 | + // Errors like NetworkOnMainThreadException etc - not clearing cached tokens. |
| 664 | + throw new AmazonClientException("failed to get new tokens from refresh",e); |
659 | 665 | } |
660 | 666 | } |
661 | 667 | throw new CognitoNotAuthorizedException("user is not authenticated"); |
@@ -2018,7 +2024,7 @@ private CognitoUserSession refreshSessionInternal(CognitoRefreshToken refreshTok |
2018 | 2024 | cognitoIdentityProviderClient.refreshTokens(refreshTokensRequest); |
2019 | 2025 | AuthenticationResultType authenticationResult = refreshTokensResult.getAuthenticationResult(); |
2020 | 2026 |
|
2021 | | - if (authenticationResult != null) { |
| 2027 | + if (authenticationResult == null) { |
2022 | 2028 | throw new CognitoNotAuthorizedException("user is not authenticated"); |
2023 | 2029 | } |
2024 | 2030 |
|
|
0 commit comments