Skip to content

Commit

Permalink
fix(Authenticator): Handling expired sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
ruisebas committed Aug 12, 2024
1 parent 6561a42 commit 087051b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Changelog

## 1.1.6 (2024-08-13)

### Bug Fixes
- **Authenticator**: Properly handling expired sessions when loading the component (#87)

## 1.1.5 (2024-07-02)

### Bug Fixes
- **Authenticator**: Settting corner radius according to the theme (#84)
- **Authenticator**: Setting corner radius according to the theme (#84)

## 1.1.4 (2024-06-07)

Expand Down
24 changes: 22 additions & 2 deletions Sources/Authenticator/Configuration/AmplifyConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,25 @@ struct AmplifyConfiguration {
}
}

var hasIdentityPool = false
if let cognitoConfiguration = configuration.value(at: "CredentialsProvider.CognitoIdentity.Default"),
case .string(let poolId) = cognitoConfiguration["PoolId"], !poolId.isEmpty {
hasIdentityPool = true
}

var hasUserPool = false
if let cognitoConfiguration = configuration.value(at: "CognitoUserPool.Default"),
case .string(let poolId) = cognitoConfiguration["PoolId"], !poolId.isEmpty {
hasUserPool = true
}

self.cognito = CognitoConfiguration(
usernameAttributes: usernameAttributes,
signupAttributes: signUpAttributes,
passwordProtectionSettings: passwordProtectionSettings,
verificationMechanisms: verificationMechanisms
verificationMechanisms: verificationMechanisms,
hasUserPool: hasUserPool,
hasIdentityPool: hasIdentityPool
)
}
}
Expand Down Expand Up @@ -179,12 +193,18 @@ struct CognitoConfiguration {
return .username
}

var hasUserPool: Bool
var hasIdentityPool: Bool

static var empty: CognitoConfiguration {
.init(
usernameAttributes: [],
signupAttributes: [],
passwordProtectionSettings: .init(minLength: 0, characterPolicy: []),
verificationMechanisms: [])
verificationMechanisms: [],
hasUserPool: false,
hasIdentityPool: false
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Authenticator/Constants/ComponentInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
import Foundation

public class ComponentInformation {
public static let version = "1.1.5"
public static let version = "1.1.6"
public static let name = "amplify-ui-swift-authenticator"
}
37 changes: 34 additions & 3 deletions Sources/Authenticator/Models/AuthenticatorState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,20 @@ public class AuthenticatorState: ObservableObject, AuthenticatorStateProtocol {
let authSession = try await authenticationService.fetchAuthSession(options: nil)

if authSession.isSignedIn {
let user = try await authenticationService.getCurrentUser()
log.info("The user is signed in, going to signedIn step")
setCurrentStep(.signedIn(user: user))
// The user has previously signed in, but validate if they still have valid credentials
guard let cognitoSession = authSession as? AWSAuthCognitoSession else {
// In the unlikely case of this happening, honour the `isSignedIn` flag
try await setSignedInStep()
return
}

if hasValidCredentials(session: cognitoSession) {
try await setSignedInStep()
} else {
log.info("The user's credentials have expired. Signing out and going to signedOut step")
_ = await Amplify.Auth.signOut()
setCurrentStep(signedOutStep)
}
} else {
log.info("The user is not signed in, going to signedOut step")
setCurrentStep(signedOutStep)
Expand All @@ -103,6 +114,26 @@ public class AuthenticatorState: ObservableObject, AuthenticatorStateProtocol {
}
}

private func hasValidCredentials(session: AWSAuthCognitoSession) -> Bool {
if configuration.hasIdentityPool, case .failure(_) = session.getIdentityId() {
log.verbose("Could not fetch Identity ID")
return false
}

if configuration.hasUserPool, case .failure(_) = session.getCognitoTokens(){
log.verbose("Could not fetch Cognito Tokens")
return false
}

return true
}

private func setSignedInStep() async throws {
log.info("The user is signed in, going to signedIn step")
let user = try await authenticationService.getCurrentUser()
setCurrentStep(.signedIn(user: user))
}

private func setUserAgentSuffix() {
guard let plugin = try? Amplify.Auth.getPlugin(for: "awsCognitoAuthPlugin") as? AWSCognitoAuthPlugin else {
log.error("Unable to retrieve the AWSCognitoAuthPlugin")
Expand Down

0 comments on commit 087051b

Please sign in to comment.