Skip to content

Commit cc3bbaf

Browse files
authored
fix(auth): remove is keychain configured check (#4129)
1 parent df880bc commit cc3bbaf

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/AWSCognitoAuthCredentialStore.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ struct AWSCognitoAuthCredentialStore {
2121

2222
// User defaults constants
2323
private let userDefaultsNameSpace = "amplify_secure_storage_scopes.awsCognitoAuthPlugin"
24-
/// This UserDefault Key is used to check if Keychain already has items stored on a fresh install
25-
/// If this flag doesn't exist, previous keychain values for Amplify would be wiped out
26-
private var isKeychainConfiguredKey: String {
27-
"\(userDefaultsNameSpace).isKeychainConfigured"
28-
}
2924
/// This UserDefaults Key is use to retrieve the stored access group to determine
3025
/// which access group the migration should happen from
3126
/// If none is found, the unshared service is used for migration and all items
@@ -67,16 +62,16 @@ struct AWSCognitoAuthCredentialStore {
6762

6863
saveStoredAccessGroup()
6964

70-
if !userDefaults.bool(forKey: isKeychainConfiguredKey) {
71-
// We can't reliably clear credentials if the Keychain has a shared access group.
72-
// This is because each app/extension has its own UserDefaults.
73-
// If a user authenticates in an app, the app or extension that shares the keychain would clear the shared credentials.
74-
// We must only clear credentials if a shared Keychain is not being used.
75-
if accessGroup == nil {
76-
try? clearAllCredentials() // clear if not using shared keychain
77-
}
78-
userDefaults.set(true, forKey: isKeychainConfiguredKey)
79-
}
65+
// NOTE: We intentionally do NOT clear keychain credentials on app reinstall.
66+
// Previously, this code checked a UserDefaults flag (isKeychainConfiguredKey) to detect
67+
// fresh installs and clear orphaned keychain items. However, this approach was unreliable
68+
// because UserDefaults can return false during iOS prewarming (background app launch after
69+
// device reboot) when protected data is not yet available. This caused valid credentials
70+
// to be incorrectly cleared, resulting in random user logouts.
71+
//
72+
// Keychain items persisting across app reinstalls is iOS's default behavior. Any stale
73+
// credentials will naturally fail authentication and trigger a proper sign-out flow.
74+
// See: https://github.com/aws-amplify/amplify-swift/issues/3972
8075

8176
restoreCredentialsOnConfigurationChanges(currentAuthConfig: authConfiguration)
8277
// Save the current configuration

0 commit comments

Comments
 (0)