Skip to content

Commit

Permalink
fix: Avoid signing out users when the token refresh failed due to con…
Browse files Browse the repository at this point in the history
…nectivity issues on startup
  • Loading branch information
ruisebas committed Dec 13, 2024
1 parent a18f030 commit 70ec07f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
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 70ec07f

Please sign in to comment.