Skip to content

Commit 6df3673

Browse files
committed
fix(auth): allow removing and adding identity pool for an already deployed app
1 parent c6df692 commit 6df3673

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ struct AWSCognitoAuthCredentialStore {
8383
let oldUserPoolConfiguration = oldAuthConfigData.getUserPoolConfiguration()
8484
let oldIdentityPoolConfiguration = oldAuthConfigData.getIdentityPoolConfiguration()
8585
let newIdentityConfigData = currentAuthConfig.getIdentityPoolConfiguration()
86+
let newUserPoolConfiguration = currentAuthConfig.getUserPoolConfiguration()
8687

87-
/// Only migrate if
88+
/// Migrate if
8889
/// - Old User Pool Config didn't exist
8990
/// - New Identity Config Data exists
9091
/// - Old Identity Pool Config == New Identity Pool Config
@@ -95,6 +96,19 @@ struct AWSCognitoAuthCredentialStore {
9596
if let oldCognitoCredentialsData = try? keychain._getData(oldNameSpace) {
9697
try? keychain._set(oldCognitoCredentialsData, key: newNameSpace)
9798
}
99+
/// Migrate if
100+
/// - Old config and new config are different
101+
/// - Old Userpool Existed
102+
/// - Old and new user pool namespacing is the same
103+
} else if oldAuthConfigData != currentAuthConfig &&
104+
oldUserPoolConfiguration != nil &&
105+
UserPoolConfigurationData.isNamespacingEqual(
106+
lhs: oldUserPoolConfiguration,
107+
rhs: newUserPoolConfiguration) {
108+
// retrieve data from the old namespace and save with the new namespace
109+
if let oldCognitoCredentialsData = try? keychain._getData(oldNameSpace) {
110+
try? keychain._set(oldCognitoCredentialsData, key: newNameSpace)
111+
}
98112
} else if oldAuthConfigData != currentAuthConfig &&
99113
oldNameSpace != newNameSpace {
100114
// Clear the old credentials

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/StateMachine/CodeGen/Data/UserPoolConfigurationData.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ struct UserPoolConfigurationData: Equatable {
5757
func getIdentityProviderName() -> String {
5858
return "cognito-idp.\(region).amazonaws.com/\(poolId)"
5959
}
60+
61+
static func isNamespacingEqual(
62+
lhs: UserPoolConfigurationData?,
63+
rhs: UserPoolConfigurationData?) -> Bool {
64+
return lhs?.poolId == rhs?.poolId
65+
&& lhs?.clientId == rhs?.clientId
66+
&& lhs?.region == rhs?.region
67+
}
6068
}
6169

6270
extension UserPoolConfigurationData: Codable { }

0 commit comments

Comments
 (0)