@@ -30,14 +30,12 @@ import com.amplifyframework.auth.exceptions.SignedOutException
30
30
import com.amplifyframework.auth.exceptions.UnknownException
31
31
import com.amplifyframework.auth.options.AuthFetchSessionOptions
32
32
import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter
33
- import com.amplifyframework.statemachine.StateMachineEvent
34
33
import com.amplifyframework.statemachine.codegen.data.AmplifyCredential
35
34
import com.amplifyframework.statemachine.codegen.errors.SessionError
36
35
import com.amplifyframework.statemachine.codegen.events.AuthorizationEvent
37
36
import com.amplifyframework.statemachine.codegen.states.AuthorizationState
38
37
import kotlinx.coroutines.flow.first
39
38
import kotlinx.coroutines.flow.mapNotNull
40
- import kotlinx.coroutines.flow.onStart
41
39
42
40
internal class FetchAuthSessionUseCase (
43
41
private val stateMachine : AuthStateMachine ,
@@ -46,82 +44,80 @@ internal class FetchAuthSessionUseCase(
46
44
suspend fun execute (options : AuthFetchSessionOptions = AuthFetchSessionOptions .defaults()): AWSCognitoAuthSession =
47
45
when (val authZState = stateMachine.getCurrentState().authZState) {
48
46
is AuthorizationState .Configured -> {
49
- listenForSessionEstablished(
50
- eventToSend = AuthorizationEvent (AuthorizationEvent .EventType .FetchUnAuthSession )
51
- )
47
+ stateMachine.send(AuthorizationEvent (AuthorizationEvent .EventType .FetchUnAuthSession ))
48
+ listenForSessionEstablished()
52
49
}
53
50
is AuthorizationState .SessionEstablished -> {
54
51
val credential = authZState.amplifyCredential
55
52
if (! credential.isValid() || options.forceRefresh) {
56
- listenForSessionEstablished(eventToSend = refreshSessionEvent(credential))
53
+ sendRefreshSessionEvent(credential)
54
+ listenForSessionEstablished()
57
55
} else {
58
56
credential.getCognitoSession()
59
57
}
60
58
}
61
59
is AuthorizationState .Error -> {
62
60
val error = authZState.exception
63
61
if (error is SessionError ) {
64
- listenForSessionEstablished(eventToSend = refreshSessionEvent(error.amplifyCredential))
62
+ sendRefreshSessionEvent(error.amplifyCredential)
63
+ listenForSessionEstablished()
65
64
} else {
66
65
throw InvalidStateException ()
67
66
}
68
67
}
69
68
else -> throw InvalidStateException ()
70
69
}
71
70
72
- private fun refreshSessionEvent (credential : AmplifyCredential ): StateMachineEvent =
71
+ private fun sendRefreshSessionEvent (credential : AmplifyCredential ) {
73
72
if (credential is AmplifyCredential .IdentityPoolFederated ) {
74
- AuthorizationEvent (
75
- AuthorizationEvent .EventType .StartFederationToIdentityPool (
76
- credential.federatedToken,
77
- credential.identityId,
78
- credential
73
+ stateMachine.send(
74
+ AuthorizationEvent (
75
+ AuthorizationEvent .EventType .StartFederationToIdentityPool (
76
+ credential.federatedToken,
77
+ credential.identityId,
78
+ credential
79
+ )
79
80
)
80
81
)
81
82
} else {
82
- AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (credential))
83
+ stateMachine.send( AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (credential) ))
83
84
}
85
+ }
84
86
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
- }
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)
110
105
}
111
106
}
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
- }
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)
120
115
}
121
116
}
122
- else -> null // no-op
123
117
}
124
- }.first()
118
+ else -> null // no-op
119
+ }
120
+ }.first()
125
121
126
122
return session
127
123
}
0 commit comments