@@ -30,12 +30,14 @@ import com.amplifyframework.auth.exceptions.SignedOutException
3030import com.amplifyframework.auth.exceptions.UnknownException
3131import com.amplifyframework.auth.options.AuthFetchSessionOptions
3232import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter
33+ import com.amplifyframework.statemachine.StateMachineEvent
3334import com.amplifyframework.statemachine.codegen.data.AmplifyCredential
3435import com.amplifyframework.statemachine.codegen.errors.SessionError
3536import com.amplifyframework.statemachine.codegen.events.AuthorizationEvent
3637import com.amplifyframework.statemachine.codegen.states.AuthorizationState
3738import kotlinx.coroutines.flow.first
3839import kotlinx.coroutines.flow.mapNotNull
40+ import kotlinx.coroutines.flow.onStart
3941
4042internal class FetchAuthSessionUseCase (
4143 private val stateMachine : AuthStateMachine ,
@@ -44,80 +46,82 @@ internal class FetchAuthSessionUseCase(
4446 suspend fun execute (options : AuthFetchSessionOptions = AuthFetchSessionOptions .defaults()): AWSCognitoAuthSession =
4547 when (val authZState = stateMachine.getCurrentState().authZState) {
4648 is AuthorizationState .Configured -> {
47- stateMachine.send(AuthorizationEvent (AuthorizationEvent .EventType .FetchUnAuthSession ))
48- listenForSessionEstablished()
49+ listenForSessionEstablished(
50+ eventToSend = AuthorizationEvent (AuthorizationEvent .EventType .FetchUnAuthSession )
51+ )
4952 }
5053 is AuthorizationState .SessionEstablished -> {
5154 val credential = authZState.amplifyCredential
5255 if (! credential.isValid() || options.forceRefresh) {
53- sendRefreshSessionEvent(credential)
54- listenForSessionEstablished()
56+ listenForSessionEstablished(eventToSend = refreshSessionEvent(credential))
5557 } else {
5658 credential.getCognitoSession()
5759 }
5860 }
5961 is AuthorizationState .Error -> {
6062 val error = authZState.exception
6163 if (error is SessionError ) {
62- sendRefreshSessionEvent(error.amplifyCredential)
63- listenForSessionEstablished()
64+ listenForSessionEstablished(eventToSend = refreshSessionEvent(error.amplifyCredential))
6465 } else {
6566 throw InvalidStateException ()
6667 }
6768 }
6869 else -> throw InvalidStateException ()
6970 }
7071
71- private fun sendRefreshSessionEvent (credential : AmplifyCredential ) {
72+ private fun refreshSessionEvent (credential : AmplifyCredential ): StateMachineEvent =
7273 if (credential is AmplifyCredential .IdentityPoolFederated ) {
73- stateMachine.send(
74- AuthorizationEvent (
75- AuthorizationEvent .EventType .StartFederationToIdentityPool (
76- credential.federatedToken,
77- credential.identityId,
78- credential
79- )
74+ AuthorizationEvent (
75+ AuthorizationEvent .EventType .StartFederationToIdentityPool (
76+ credential.federatedToken,
77+ credential.identityId,
78+ credential
8079 )
8180 )
8281 } else {
83- stateMachine.send( AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (credential) ))
82+ AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (credential))
8483 }
85- }
8684
87- private suspend fun listenForSessionEstablished (): AWSCognitoAuthSession {
88- val session = stateMachine.stateTransitions.mapNotNull { authState ->
89- when (val authZState = authState.authZState) {
90- is AuthorizationState .SessionEstablished -> authZState.amplifyCredential.getCognitoSession()
91- is AuthorizationState .Error -> {
92- when (val error = authZState.exception) {
93- is SessionError -> {
94- when (val innerException = error.exception) {
95- is SignedOutException -> error.amplifyCredential.getCognitoSession(innerException)
96- is ServiceException -> error.amplifyCredential.getCognitoSession(innerException)
97- is NotAuthorizedException -> error.amplifyCredential.getCognitoSession(innerException)
98- is SessionExpiredException -> {
99- emitter.sendHubEvent(AuthChannelEventName .SESSION_EXPIRED .toString())
100- error.amplifyCredential.getCognitoSession(innerException)
101- }
102- else -> {
103- val errorResult = UnknownException (" Fetch auth session failed." , innerException)
104- error.amplifyCredential.getCognitoSession(errorResult)
85+ private suspend fun listenForSessionEstablished (eventToSend : StateMachineEvent ): AWSCognitoAuthSession {
86+ val session = stateMachine.stateTransitions
87+ .onStart {
88+ stateMachine.send(eventToSend)
89+ }
90+ .mapNotNull { authState ->
91+ when (val authZState = authState.authZState) {
92+ is AuthorizationState .SessionEstablished -> authZState.amplifyCredential.getCognitoSession()
93+ is AuthorizationState .Error -> {
94+ when (val error = authZState.exception) {
95+ is SessionError -> {
96+ when (val innerException = error.exception) {
97+ is SignedOutException -> error.amplifyCredential.getCognitoSession(innerException)
98+ is ServiceException -> error.amplifyCredential.getCognitoSession(innerException)
99+ is NotAuthorizedException -> error.amplifyCredential.getCognitoSession(
100+ innerException
101+ )
102+ is SessionExpiredException -> {
103+ emitter.sendHubEvent(AuthChannelEventName .SESSION_EXPIRED .toString())
104+ error.amplifyCredential.getCognitoSession(innerException)
105+ }
106+ else -> {
107+ val errorResult = UnknownException (" Fetch auth session failed." , innerException)
108+ error.amplifyCredential.getCognitoSession(errorResult)
109+ }
105110 }
106111 }
107- }
108- is ConfigurationException -> {
109- val errorResult = InvalidAccountTypeException (error )
110- AmplifyCredential . Empty .getCognitoSession(errorResult)
111- }
112- else -> {
113- val errorResult = UnknownException ( " Fetch auth session failed. " , error )
114- AmplifyCredential . Empty .getCognitoSession(errorResult)
112+ is ConfigurationException -> {
113+ val errorResult = InvalidAccountTypeException (error)
114+ AmplifyCredential . Empty .getCognitoSession(errorResult )
115+ }
116+ else -> {
117+ val errorResult = UnknownException ( " Fetch auth session failed. " , error)
118+ AmplifyCredential . Empty .getCognitoSession(errorResult )
119+ }
115120 }
116121 }
122+ else -> null // no-op
117123 }
118- else -> null // no-op
119- }
120- }.first()
124+ }.first()
121125
122126 return session
123127 }
0 commit comments