Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avoiding signing out users when the token refresh fails due to no connectivity #104

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading