Skip to content

Commit 23d6e6d

Browse files
authored
fix(auth): Emit SIGNED_IN event after autoSignIn (#3068)
1 parent 04209f9 commit 23d6e6d

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AuthUseCaseFactory.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ import com.amplifyframework.auth.cognito.AuthStateMachine
2020
import com.amplifyframework.auth.cognito.RealAWSCognitoAuthPlugin
2121
import com.amplifyframework.auth.cognito.helpers.WebAuthnHelper
2222
import com.amplifyframework.auth.cognito.requireIdentityProviderClient
23-
import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter
2423

2524
internal class AuthUseCaseFactory(
2625
private val plugin: RealAWSCognitoAuthPlugin,
2726
private val authEnvironment: AuthEnvironment,
28-
private val stateMachine: AuthStateMachine,
29-
private val hubEmitter: AuthHubEventEmitter = AuthHubEventEmitter()
27+
private val stateMachine: AuthStateMachine
3028
) {
3129

3230
fun fetchAuthSession() = FetchAuthSessionUseCase(plugin)
@@ -143,8 +141,7 @@ internal class AuthUseCaseFactory(
143141
)
144142

145143
fun autoSignIn() = AutoSignInUseCase(
146-
stateMachine = stateMachine,
147-
hubEmitter = hubEmitter
144+
stateMachine = stateMachine
148145
)
149146

150147
fun fetchMfaPreference() = FetchMfaPreferenceUseCase(

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AutoSignInUseCase.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ import com.amplifyframework.statemachine.codegen.states.AuthorizationState
3333
import com.amplifyframework.statemachine.codegen.states.SignInState
3434
import com.amplifyframework.statemachine.codegen.states.SignUpState
3535
import kotlinx.coroutines.flow.first
36+
import kotlinx.coroutines.flow.mapNotNull
3637
import kotlinx.coroutines.flow.onStart
3738
import kotlinx.coroutines.flow.transformWhile
3839

3940
internal class AutoSignInUseCase(
4041
private val stateMachine: AuthStateMachine,
41-
private val hubEmitter: AuthHubEventEmitter
42+
private val hubEmitter: AuthHubEventEmitter = AuthHubEventEmitter()
4243
) {
4344
suspend fun execute(): AuthSignInResult {
4445
val authState = waitForSignedOutState()
@@ -88,7 +89,7 @@ internal class AutoSignInUseCase(
8889
val event = AuthenticationEvent(AuthenticationEvent.EventType.SignInRequested(signInData))
8990
stateMachine.send(event)
9091
}
91-
.transformWhile { authState ->
92+
.mapNotNull { authState ->
9293
val authNState = authState.authNState
9394
val authZState = authState.authZState
9495
when {
@@ -97,7 +98,7 @@ internal class AutoSignInUseCase(
9798
if (signInState is SignInState.Error) {
9899
throw CognitoAuthExceptionConverter.lookup(signInState.exception, "Sign in failed.")
99100
}
100-
true
101+
null
101102
}
102103
authNState is AuthenticationState.SignedIn &&
103104
authZState is AuthorizationState.SessionEstablished -> {
@@ -114,11 +115,10 @@ internal class AutoSignInUseCase(
114115
null
115116
)
116117
)
117-
emit(authSignInResult)
118118
hubEmitter.sendHubEvent(AuthChannelEventName.SIGNED_IN.toString())
119-
false
119+
authSignInResult
120120
}
121-
else -> true
121+
else -> null
122122
}
123123
}.first()
124124
return result

aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/usecases/AutoSignInUseCaseTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package com.amplifyframework.auth.cognito.usecases
1717

18+
import com.amplifyframework.auth.AuthChannelEventName
1819
import com.amplifyframework.auth.cognito.AuthStateMachine
1920
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
2021
import com.amplifyframework.auth.cognito.testUtil.withAuthEvent
@@ -137,4 +138,29 @@ class AutoSignInUseCaseTest {
137138
result.isSignedIn shouldBe true
138139
result.nextStep.signInStep shouldBe AuthSignInStep.DONE
139140
}
141+
142+
@Test
143+
fun `emits auth hub event`() = runTest {
144+
stateFlow.value = authState(
145+
AuthenticationState.SignedOut(mockk()),
146+
authSignUpState = SignUpState.SignedUp(signUpData, mockk())
147+
)
148+
149+
val deferred = backgroundScope.async { useCase.execute() }
150+
runCurrent()
151+
152+
stateFlow.value = authState(AuthenticationState.SigningIn())
153+
runCurrent()
154+
155+
stateFlow.value = authState(
156+
AuthenticationState.SignedIn(mockk(), mockk()),
157+
AuthorizationState.SessionEstablished(mockk())
158+
)
159+
160+
deferred.await()
161+
162+
verify {
163+
hubEmitter.sendHubEvent(AuthChannelEventName.SIGNED_IN.toString())
164+
}
165+
}
140166
}

0 commit comments

Comments
 (0)