Skip to content

Commit 0fcd480

Browse files
authored
fix(Authenticator): Handling expired sessions (#87)
1 parent 6561a42 commit 0fcd480

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# Changelog
22

3+
## 1.1.6 (2024-08-13)
4+
5+
### Bug Fixes
6+
- **Authenticator**: Properly handling expired sessions when loading the component (#87)
7+
38
## 1.1.5 (2024-07-02)
49

510
### Bug Fixes
6-
- **Authenticator**: Settting corner radius according to the theme (#84)
11+
- **Authenticator**: Setting corner radius according to the theme (#84)
712

813
## 1.1.4 (2024-06-07)
914

Sources/Authenticator/Configuration/AmplifyConfiguration.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,25 @@ struct AmplifyConfiguration {
106106
}
107107
}
108108

109+
var hasIdentityPool = false
110+
if let cognitoConfiguration = configuration.value(at: "CredentialsProvider.CognitoIdentity.Default"),
111+
case .string(let poolId) = cognitoConfiguration["PoolId"], !poolId.isEmpty {
112+
hasIdentityPool = true
113+
}
114+
115+
var hasUserPool = false
116+
if let cognitoConfiguration = configuration.value(at: "CognitoUserPool.Default"),
117+
case .string(let poolId) = cognitoConfiguration["PoolId"], !poolId.isEmpty {
118+
hasUserPool = true
119+
}
120+
109121
self.cognito = CognitoConfiguration(
110122
usernameAttributes: usernameAttributes,
111123
signupAttributes: signUpAttributes,
112124
passwordProtectionSettings: passwordProtectionSettings,
113-
verificationMechanisms: verificationMechanisms
125+
verificationMechanisms: verificationMechanisms,
126+
hasUserPool: hasUserPool,
127+
hasIdentityPool: hasIdentityPool
114128
)
115129
}
116130
}
@@ -179,12 +193,18 @@ struct CognitoConfiguration {
179193
return .username
180194
}
181195

196+
var hasUserPool: Bool
197+
var hasIdentityPool: Bool
198+
182199
static var empty: CognitoConfiguration {
183200
.init(
184201
usernameAttributes: [],
185202
signupAttributes: [],
186203
passwordProtectionSettings: .init(minLength: 0, characterPolicy: []),
187-
verificationMechanisms: [])
204+
verificationMechanisms: [],
205+
hasUserPool: false,
206+
hasIdentityPool: false
207+
)
188208
}
189209
}
190210

Sources/Authenticator/Constants/ComponentInformation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
import Foundation
99

1010
public class ComponentInformation {
11-
public static let version = "1.1.5"
11+
public static let version = "1.1.6"
1212
public static let name = "amplify-ui-swift-authenticator"
1313
}

Sources/Authenticator/Models/AuthenticatorState.swift

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,16 @@ public class AuthenticatorState: ObservableObject, AuthenticatorStateProtocol {
8888
let authSession = try await authenticationService.fetchAuthSession(options: nil)
8989

9090
if authSession.isSignedIn {
91-
let user = try await authenticationService.getCurrentUser()
92-
log.info("The user is signed in, going to signedIn step")
93-
setCurrentStep(.signedIn(user: user))
91+
// The user has previously signed in, but validate if the session is still valid
92+
if isSessionValid(authSession) {
93+
log.info("The user is signed in, going to signedIn step")
94+
let user = try await authenticationService.getCurrentUser()
95+
setCurrentStep(.signedIn(user: user))
96+
} else {
97+
log.info("The user's credentials have expired. Signing out and going to signedOut step")
98+
_ = await Amplify.Auth.signOut()
99+
setCurrentStep(signedOutStep)
100+
}
94101
} else {
95102
log.info("The user is not signed in, going to signedOut step")
96103
setCurrentStep(signedOutStep)
@@ -103,6 +110,25 @@ public class AuthenticatorState: ObservableObject, AuthenticatorStateProtocol {
103110
}
104111
}
105112

113+
private func isSessionValid(_ session: AuthSession) -> Bool {
114+
guard let cognitoSession = session as? AWSAuthCognitoSession else {
115+
// Consider non-Cognito sessions to be valid if it's signed in
116+
return session.isSignedIn
117+
}
118+
119+
if configuration.hasIdentityPool, case .failure(_) = cognitoSession.getIdentityId() {
120+
log.verbose("Could not fetch Identity ID")
121+
return false
122+
}
123+
124+
if configuration.hasUserPool, case .failure(_) = cognitoSession.getCognitoTokens(){
125+
log.verbose("Could not fetch Cognito Tokens")
126+
return false
127+
}
128+
129+
return true
130+
}
131+
106132
private func setUserAgentSuffix() {
107133
guard let plugin = try? Amplify.Auth.getPlugin(for: "awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin else {
108134
log.error("Unable to retrieve the AWSCognitoAuthPlugin")

Tests/AuthenticatorTests/Mocks/MockAuthenticatorState.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class MockAuthenticatorState: AuthenticatorStateProtocol {
1515
usernameAttributes: [],
1616
signupAttributes: [],
1717
passwordProtectionSettings: .init(minLength: 0, characterPolicy: []),
18-
verificationMechanisms: []
18+
verificationMechanisms: [],
19+
hasUserPool: true,
20+
hasIdentityPool: true
1921
)
2022

2123
var setCurrentStepCount = 0

0 commit comments

Comments
 (0)