Skip to content

Commit fd86328

Browse files
authored
fix: Avoiding signing out users when the token refresh fails due to no connectivity (#104)
1 parent a18f030 commit fd86328

File tree

4 files changed

+40
-17
lines changed

4 files changed

+40
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 1.2.3 (2024-12-16)
4+
5+
### Bug Fixes
6+
- **Theming**: Adding support for customizing the TextFields' input and placeholder foreground colours (#100)
7+
- **Authenticator**: Avoiding signing out users when the token refresh fails due to no connectivity (#104)
8+
39
## 1.2.2 (2024-11-26)
410

511
### Misc. Updates
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Amplify
9+
import Foundation
10+
11+
extension AuthError {
12+
var isConnectivityError: Bool {
13+
guard let error = underlyingError as? NSError else {
14+
return false
15+
}
16+
17+
let networkErrorCodes = [
18+
NSURLErrorCannotFindHost,
19+
NSURLErrorCannotConnectToHost,
20+
NSURLErrorNetworkConnectionLost,
21+
NSURLErrorDNSLookupFailed,
22+
NSURLErrorNotConnectedToInternet
23+
]
24+
return networkErrorCodes.contains(where: { $0 == error.code })
25+
}
26+
}

Sources/Authenticator/Models/AuthenticatorState.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,17 @@ public class AuthenticatorState: ObservableObject, AuthenticatorStateProtocol {
116116
return session.isSignedIn
117117
}
118118

119-
if configuration.hasIdentityPool, case .failure(_) = cognitoSession.getIdentityId() {
119+
// If the failures are caused due to connectivity errors, consider the session still valid
120+
if configuration.hasIdentityPool,
121+
case .failure(let authError) = cognitoSession.getIdentityId(),
122+
!authError.isConnectivityError {
120123
log.verbose("Could not fetch Identity ID")
121124
return false
122125
}
123126

124-
if configuration.hasUserPool, case .failure(_) = cognitoSession.getCognitoTokens(){
127+
if configuration.hasUserPool,
128+
case .failure(let authError) = cognitoSession.getCognitoTokens(),
129+
!authError.isConnectivityError {
125130
log.verbose("Could not fetch Cognito Tokens")
126131
return false
127132
}

Sources/Authenticator/States/AuthenticatorBaseState.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public class AuthenticatorBaseState: ObservableObject {
227227
}
228228

229229
// First check if the underlying error is a connectivity one
230-
if isConnectivityError(error.underlyingError) {
230+
if error.isConnectivityError {
231231
log.verbose("The error is identified as a connectivity issue, displaying the corresponding localized string.")
232232
return "authenticator.cognitoError.network".localized()
233233
}
@@ -248,20 +248,6 @@ public class AuthenticatorBaseState: ObservableObject {
248248
log.verbose("No localizable string was found for error of type '\(cognitoError)'")
249249
return nil
250250
}
251-
252-
private func isConnectivityError(_ error: Error?) -> Bool {
253-
guard let error = error as? NSError else {
254-
return false
255-
}
256-
let networkErrorCodes = [
257-
NSURLErrorCannotFindHost,
258-
NSURLErrorCannotConnectToHost,
259-
NSURLErrorNetworkConnectionLost,
260-
NSURLErrorDNSLookupFailed,
261-
NSURLErrorNotConnectedToInternet
262-
]
263-
return networkErrorCodes.contains(where: { $0 == error.code })
264-
}
265251
}
266252

267253
extension AuthenticatorBaseState: Equatable {

0 commit comments

Comments
 (0)