Skip to content

Commit

Permalink
fix: Avoiding signing out users when the token refresh fails due to n…
Browse files Browse the repository at this point in the history
…o connectivity (#104)
  • Loading branch information
sebaland authored Dec 16, 2024
1 parent a18f030 commit fd86328
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.2.3 (2024-12-16)

### Bug Fixes
- **Theming**: Adding support for customizing the TextFields' input and placeholder foreground colours (#100)
- **Authenticator**: Avoiding signing out users when the token refresh fails due to no connectivity (#104)

## 1.2.2 (2024-11-26)

### Misc. Updates
Expand Down
26 changes: 26 additions & 0 deletions Sources/Authenticator/Extensions/AuthError+Connectivity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Amplify
import Foundation

extension AuthError {
var isConnectivityError: Bool {
guard let error = underlyingError as? NSError else {
return false
}

let networkErrorCodes = [
NSURLErrorCannotFindHost,
NSURLErrorCannotConnectToHost,
NSURLErrorNetworkConnectionLost,
NSURLErrorDNSLookupFailed,
NSURLErrorNotConnectedToInternet
]
return networkErrorCodes.contains(where: { $0 == error.code })
}
}
9 changes: 7 additions & 2 deletions Sources/Authenticator/Models/AuthenticatorState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ public class AuthenticatorState: ObservableObject, AuthenticatorStateProtocol {
return session.isSignedIn
}

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

if configuration.hasUserPool, case .failure(_) = cognitoSession.getCognitoTokens(){
if configuration.hasUserPool,
case .failure(let authError) = cognitoSession.getCognitoTokens(),
!authError.isConnectivityError {
log.verbose("Could not fetch Cognito Tokens")
return false
}
Expand Down
16 changes: 1 addition & 15 deletions Sources/Authenticator/States/AuthenticatorBaseState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public class AuthenticatorBaseState: ObservableObject {
}

// First check if the underlying error is a connectivity one
if isConnectivityError(error.underlyingError) {
if error.isConnectivityError {
log.verbose("The error is identified as a connectivity issue, displaying the corresponding localized string.")
return "authenticator.cognitoError.network".localized()
}
Expand All @@ -248,20 +248,6 @@ public class AuthenticatorBaseState: ObservableObject {
log.verbose("No localizable string was found for error of type '\(cognitoError)'")
return nil
}

private func isConnectivityError(_ error: Error?) -> Bool {
guard let error = error as? NSError else {
return false
}
let networkErrorCodes = [
NSURLErrorCannotFindHost,
NSURLErrorCannotConnectToHost,
NSURLErrorNetworkConnectionLost,
NSURLErrorDNSLookupFailed,
NSURLErrorNotConnectedToInternet
]
return networkErrorCodes.contains(where: { $0 == error.code })
}
}

extension AuthenticatorBaseState: Equatable {
Expand Down

0 comments on commit fd86328

Please sign in to comment.