Skip to content

Commit 656838b

Browse files
committed
Add retry for getCurrentUser
1 parent 59bbe0b commit 656838b

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

authenticator/src/main/java/com/amplifyframework/ui/authenticator/AuthenticatorViewModel.kt

+9-18
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ import kotlinx.coroutines.launch
9090
import kotlinx.coroutines.withContext
9191
import org.jetbrains.annotations.VisibleForTesting
9292

93-
internal class AuthenticatorViewModel(
94-
application: Application,
95-
private val authProvider: AuthProvider
96-
) : AndroidViewModel(application) {
93+
internal class AuthenticatorViewModel(application: Application, private val authProvider: AuthProvider) :
94+
AndroidViewModel(application) {
9795

9896
// Constructor for compose viewModels provider
9997
constructor(application: Application) : this(application, RealAuthProvider())
@@ -363,22 +361,15 @@ internal class AuthenticatorViewModel(
363361
)
364362
}
365363

366-
private suspend fun handleEmailMfaSetupRequired(
367-
username: String,
368-
password: String
369-
) {
364+
private suspend fun handleEmailMfaSetupRequired(username: String, password: String) {
370365
moveTo(
371366
stateFactory.newSignInContinueWithEmailSetupState(
372367
onSubmit = { mfaType -> confirmSignIn(username, password, mfaType) }
373368
)
374369
)
375370
}
376371

377-
private suspend fun handleMfaSelectionRequired(
378-
username: String,
379-
password: String,
380-
allowedMfaTypes: Set<MFAType>?
381-
) {
372+
private suspend fun handleMfaSelectionRequired(username: String, password: String, allowedMfaTypes: Set<MFAType>?) {
382373
if (allowedMfaTypes.isNullOrEmpty()) {
383374
handleGeneralFailure(AuthException("Missing allowedMfaTypes", "Please open a bug with Amplify"))
384375
return
@@ -500,10 +491,7 @@ internal class AuthenticatorViewModel(
500491
}.join()
501492
}
502493

503-
private suspend fun handleResetPasswordSuccess(
504-
username: String,
505-
result: AuthResetPasswordResult
506-
) {
494+
private suspend fun handleResetPasswordSuccess(username: String, result: AuthResetPasswordResult) {
507495
when (result.nextStep.resetPasswordStep) {
508496
AuthResetPasswordStep.DONE -> handlePasswordResetComplete()
509497
AuthResetPasswordStep.CONFIRM_RESET_PASSWORD_WITH_CODE -> {
@@ -634,7 +622,10 @@ internal class AuthenticatorViewModel(
634622
logger.error("Current signed in user session has expired, signing out.")
635623
signOut()
636624
} else {
637-
handleGeneralFailure(result.error)
625+
handleRetryableGeneralFailure(
626+
error = result.error,
627+
onRetry = { viewModelScope.launch { handleSignedIn() }.join() }
628+
)
638629
}
639630
}
640631

authenticator/src/test/java/com/amplifyframework/ui/authenticator/AuthenticatorViewModelTest.kt

+21
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ class AuthenticatorViewModelTest {
165165
}
166166
}
167167

168+
@Test
169+
fun `getCurrentUser error can be retried`() = runTest {
170+
coEvery { authProvider.fetchAuthSession() } returns Success(mockAuthSession(isSignedIn = true))
171+
coEvery { authProvider.getCurrentUser() } returns Error(mockAuthException()) andThen Success(mockAuthUser())
172+
173+
viewModel.start(mockAuthenticatorConfiguration())
174+
advanceUntilIdle()
175+
176+
val state = viewModel.stepState.value.shouldBeInstanceOf<ErrorState>()
177+
state.retry()
178+
advanceUntilIdle()
179+
180+
viewModel.currentStep shouldBe AuthenticatorStep.SignedIn
181+
coVerify(exactly = 1) {
182+
authProvider.fetchAuthSession()
183+
}
184+
coVerify(exactly = 2) {
185+
authProvider.getCurrentUser()
186+
}
187+
}
188+
168189
@Test
169190
fun `when already signed in during start the initial state should be signed in`() = runTest {
170191
coEvery { authProvider.fetchAuthSession() } returns Success(mockAuthSession(isSignedIn = true))

0 commit comments

Comments
 (0)