@@ -35,14 +35,9 @@ import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeEx
3535import com.amplifyframework.auth.cognito.exceptions.service.UserCancelledException
3636import com.amplifyframework.auth.cognito.helpers.HostedUIHelper
3737import com.amplifyframework.auth.cognito.helpers.identityProviderName
38- import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignOutOptions
3938import com.amplifyframework.auth.cognito.options.AWSCognitoAuthWebUISignInOptions
4039import com.amplifyframework.auth.cognito.options.FederateToIdentityPoolOptions
41- import com.amplifyframework.auth.cognito.result.AWSCognitoAuthSignOutResult
4240import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
43- import com.amplifyframework.auth.cognito.result.GlobalSignOutError
44- import com.amplifyframework.auth.cognito.result.HostedUIError
45- import com.amplifyframework.auth.cognito.result.RevokeTokenError
4641import com.amplifyframework.auth.exceptions.ConfigurationException
4742import com.amplifyframework.auth.exceptions.InvalidStateException
4843import com.amplifyframework.auth.exceptions.NotAuthorizedException
@@ -51,13 +46,10 @@ import com.amplifyframework.auth.exceptions.SessionExpiredException
5146import com.amplifyframework.auth.exceptions.SignedOutException
5247import com.amplifyframework.auth.exceptions.UnknownException
5348import com.amplifyframework.auth.options.AuthFetchSessionOptions
54- import com.amplifyframework.auth.options.AuthSignOutOptions
5549import com.amplifyframework.auth.options.AuthWebUISignInOptions
5650import com.amplifyframework.auth.result.AuthSignInResult
57- import com.amplifyframework.auth.result.AuthSignOutResult
5851import com.amplifyframework.auth.result.step.AuthNextSignInStep
5952import com.amplifyframework.auth.result.step.AuthSignInStep
60- import com.amplifyframework.core.Action
6153import com.amplifyframework.core.Amplify
6254import com.amplifyframework.core.Consumer
6355import com.amplifyframework.hub.HubChannel
@@ -69,7 +61,6 @@ import com.amplifyframework.statemachine.codegen.data.FederatedToken
6961import com.amplifyframework.statemachine.codegen.data.HostedUIErrorData
7062import com.amplifyframework.statemachine.codegen.data.SignInData
7163import com.amplifyframework.statemachine.codegen.data.SignInMethod
72- import com.amplifyframework.statemachine.codegen.data.SignOutData
7364import com.amplifyframework.statemachine.codegen.errors.SessionError
7465import com.amplifyframework.statemachine.codegen.events.AuthEvent
7566import com.amplifyframework.statemachine.codegen.events.AuthenticationEvent
@@ -493,114 +484,6 @@ internal class RealAWSCognitoAuthPlugin(
493484 )
494485 }
495486
496- fun signOut (onComplete : Consumer <AuthSignOutResult >) {
497- signOut(AuthSignOutOptions .builder().build(), onComplete)
498- }
499-
500- fun signOut (options : AuthSignOutOptions , onComplete : Consumer <AuthSignOutResult >) {
501- authStateMachine.getCurrentState { authState ->
502- when (authState.authNState) {
503- is AuthenticationState .NotConfigured ->
504- onComplete.accept(AWSCognitoAuthSignOutResult .CompleteSignOut )
505- // Continue sign out and clear auth or guest credentials
506- is AuthenticationState .SignedIn , is AuthenticationState .SignedOut -> {
507- // Send SignOut event here instead of OnSubscribedCallback handler to ensure we do not fire
508- // onComplete immediately, which would happen if calling signOut while signed out
509- val event = AuthenticationEvent (
510- AuthenticationEvent .EventType .SignOutRequested (
511- SignOutData (
512- options.isGlobalSignOut,
513- (options as ? AWSCognitoAuthSignOutOptions )?.browserPackage
514- )
515- )
516- )
517- authStateMachine.send(event)
518- _signOut (onComplete = onComplete)
519- }
520- is AuthenticationState .FederatedToIdentityPool -> {
521- onComplete.accept(
522- AWSCognitoAuthSignOutResult .FailedSignOut (
523- InvalidStateException (
524- " The user is currently federated to identity pool. " +
525- " You must call clearFederationToIdentityPool to clear credentials."
526- )
527- )
528- )
529- }
530- else -> onComplete.accept(
531- AWSCognitoAuthSignOutResult .FailedSignOut (InvalidStateException ())
532- )
533- }
534- }
535- }
536-
537- private fun _signOut (sendHubEvent : Boolean = true, onComplete : Consumer <AuthSignOutResult >) {
538- val token = StateChangeListenerToken ()
539- var cancellationException: UserCancelledException ? = null
540- authStateMachine.listen(
541- token,
542- { authState ->
543- if (authState is AuthState .Configured ) {
544- val (authNState, authZState) = authState
545- when {
546- authNState is AuthenticationState .SignedOut && authZState is AuthorizationState .Configured -> {
547- authStateMachine.cancel(token)
548- if (authNState.signedOutData.hasError) {
549- val signedOutData = authNState.signedOutData
550- onComplete.accept(
551- AWSCognitoAuthSignOutResult .PartialSignOut (
552- hostedUIError = signedOutData.hostedUIErrorData?.let { HostedUIError (it) },
553- globalSignOutError = signedOutData.globalSignOutErrorData?.let {
554- GlobalSignOutError (it)
555- },
556- revokeTokenError = signedOutData.revokeTokenErrorData?.let {
557- RevokeTokenError (
558- it
559- )
560- }
561- )
562- )
563- if (sendHubEvent) {
564- sendHubEvent(AuthChannelEventName .SIGNED_OUT .toString())
565- }
566- } else {
567- onComplete.accept(AWSCognitoAuthSignOutResult .CompleteSignOut )
568- if (sendHubEvent) {
569- sendHubEvent(AuthChannelEventName .SIGNED_OUT .toString())
570- }
571- }
572- }
573- authNState is AuthenticationState .Error -> {
574- authStateMachine.cancel(token)
575- onComplete.accept(
576- AWSCognitoAuthSignOutResult .FailedSignOut (
577- CognitoAuthExceptionConverter .lookup(authNState.exception, " Sign out failed." )
578- )
579- )
580- }
581- authNState is AuthenticationState .SigningOut -> {
582- val state = authNState.signOutState
583- if (state is SignOutState .Error && state.exception is UserCancelledException ) {
584- cancellationException = state.exception
585- }
586- }
587- authNState is AuthenticationState .SignedIn && cancellationException != null -> {
588- authStateMachine.cancel(token)
589- cancellationException?.let {
590- onComplete.accept(AWSCognitoAuthSignOutResult .FailedSignOut (it))
591- }
592- }
593- else -> {
594- // No - op
595- }
596- }
597- }
598- },
599- {
600- }
601- )
602- }
603-
604487 private fun addAuthStateChangeListener () {
605488 authStateMachine.listen(
606489 StateChangeListenerToken (),
@@ -732,46 +615,6 @@ internal class RealAWSCognitoAuthPlugin(
732615 )
733616 }
734617
735- fun clearFederationToIdentityPool (onSuccess : Action , onError : Consumer <AuthException >) {
736- authStateMachine.getCurrentState { authState ->
737- val authNState = authState.authNState
738- val authZState = authState.authZState
739- when {
740- authState is AuthState .Configured &&
741- (
742- authNState is AuthenticationState .FederatedToIdentityPool &&
743- authZState is AuthorizationState .SessionEstablished
744- ) ||
745- (
746- authZState is AuthorizationState .Error &&
747- authZState.exception is SessionError &&
748- authZState.exception.amplifyCredential is AmplifyCredential .IdentityPoolFederated
749- ) -> {
750- val event = AuthenticationEvent (AuthenticationEvent .EventType .ClearFederationToIdentityPool ())
751- authStateMachine.send(event)
752- _clearFederationToIdentityPool (onSuccess, onError)
753- }
754- else -> {
755- onError.accept(InvalidStateException (" Clearing of federation failed." ))
756- }
757- }
758- }
759- }
760-
761- private fun _clearFederationToIdentityPool (onSuccess : Action , onError : Consumer <AuthException >) {
762- _signOut (sendHubEvent = false ) {
763- when (it) {
764- is AWSCognitoAuthSignOutResult .FailedSignOut -> {
765- onError.accept(it.exception)
766- }
767- else -> {
768- onSuccess.call()
769- sendHubEvent(AWSCognitoAuthChannelEventName .FEDERATION_TO_IDENTITY_POOL_CLEARED .toString())
770- }
771- }
772- }
773- }
774-
775618 private fun sendHubEvent (eventName : String ) {
776619 Amplify .Hub .publish(HubChannel .AUTH , HubEvent .create(eventName))
777620 }
0 commit comments