-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Auth): Hub events for signedIn signedOut and sessionExpire (#457)
- Loading branch information
Showing
8 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
public extension HubPayload.EventName.Auth { | ||
|
||
static let signedIn = "Auth.signedIn" | ||
|
||
static let signedOut = "Auth.signedOut" | ||
|
||
static let sessionExpired = "Auth.sessionExpired" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
AmplifyPlugins/Auth/AWSAuthPlugin/HubEvents/AuthHubEventBehavior.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
protocol AuthHubEventBehavior { | ||
|
||
func sendUserSignedInEvent() | ||
|
||
func sendUserSignedOutEvent() | ||
|
||
func sendSessionExpiredEvent() | ||
} |
98 changes: 98 additions & 0 deletions
98
AmplifyPlugins/Auth/AWSAuthPlugin/HubEvents/AuthHubEventHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import AWSPluginsCore | ||
|
||
class AuthHubEventHandler: AuthHubEventBehavior { | ||
|
||
var lastSendEventName: HubPayloadEventName? | ||
|
||
init() { | ||
setupHubEvents() | ||
} | ||
|
||
func sendUserSignedInEvent() { | ||
dispatchAuthEvent(HubPayload.EventName.Auth.signedIn) | ||
} | ||
|
||
func sendUserSignedOutEvent() { | ||
dispatchAuthEvent(HubPayload.EventName.Auth.signedOut) | ||
} | ||
|
||
func sendSessionExpiredEvent() { | ||
dispatchAuthEvent(HubPayload.EventName.Auth.sessionExpired) | ||
} | ||
|
||
private func setupHubEvents() { | ||
|
||
_ = Amplify.Hub.listen(to: .auth) {[weak self] payload in | ||
switch payload.eventName { | ||
|
||
case HubPayload.EventName.Auth.signIn: | ||
guard let event = payload.data as? AWSAuthSignInOperation.OperationResult, | ||
case let .success(result) = event else { | ||
return | ||
} | ||
self?.handleSignInEvent(result) | ||
|
||
case HubPayload.EventName.Auth.confirmSignIn: | ||
guard let event = payload.data as? AWSAuthConfirmSignInOperation.OperationResult, | ||
case let .success(result) = event else { | ||
return | ||
} | ||
self?.handleSignInEvent(result) | ||
|
||
case HubPayload.EventName.Auth.signOut: | ||
guard let event = payload.data as? AWSAuthSignOutOperation.OperationResult, | ||
case .success(_) = event else { | ||
return | ||
} | ||
self?.sendUserSignedOutEvent() | ||
|
||
case HubPayload.EventName.Auth.fetchSession: | ||
guard let event = payload.data as? AWSAuthFetchSessionOperation.OperationResult, | ||
case let .success(result) = event else { | ||
return | ||
} | ||
self?.handleSessionEvent(result) | ||
|
||
default: | ||
break | ||
} | ||
} | ||
} | ||
|
||
private func handleSignInEvent(_ signInResult: AuthSignInResult) { | ||
guard signInResult.isSignedIn else { | ||
return | ||
} | ||
sendUserSignedInEvent() | ||
} | ||
|
||
private func handleSessionEvent(_ sessionResult: AuthSession) { | ||
guard let tokensProvider = sessionResult as? AuthCognitoTokensProvider, | ||
case let .failure(authError) = tokensProvider.getCognitoTokens() else { | ||
return | ||
} | ||
|
||
guard case let .service(_, _, cognitoError as AWSCognitoAuthError) = authError, | ||
cognitoError == .sessionExpired else { | ||
return | ||
} | ||
|
||
sendSessionExpiredEvent() | ||
} | ||
|
||
private func dispatchAuthEvent(_ eventName: String) { | ||
if eventName != lastSendEventName { | ||
lastSendEventName = eventName | ||
Amplify.Hub.dispatch(to: .auth, payload: HubPayload(eventName: eventName)) | ||
} | ||
} | ||
|
||
} |