File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed
main/java/com/amplifyframework/ui/authenticator
test/java/com/amplifyframework/ui/authenticator Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ import com.amplifyframework.auth.cognito.exceptions.service.UserNotConfirmedExce
3535import com.amplifyframework.auth.cognito.exceptions.service.UserNotFoundException
3636import com.amplifyframework.auth.cognito.exceptions.service.UsernameExistsException
3737import com.amplifyframework.auth.exceptions.NotAuthorizedException
38+ import com.amplifyframework.auth.exceptions.SessionExpiredException
3839import com.amplifyframework.auth.exceptions.UnknownException
3940import com.amplifyframework.auth.options.AuthSignUpOptions
4041import com.amplifyframework.auth.result.AuthResetPasswordResult
@@ -573,7 +574,17 @@ internal class AuthenticatorViewModel(
573574 private suspend fun handleSignedIn () {
574575 logger.debug(" Log in successful, getting current user" )
575576 when (val result = authProvider.getCurrentUser()) {
576- is AmplifyResult .Error -> handleGeneralFailure(result.error)
577+ is AmplifyResult .Error -> {
578+ if (result.error is SessionExpiredException ) {
579+ logger.error(result.error.toString())
580+ logger.error(" Current signed in user session has expired, signing out." )
581+ signOut()
582+ moveTo(AuthenticatorStep .SignIn )
583+ } else {
584+ handleGeneralFailure(result.error)
585+ }
586+ }
587+
577588 is AmplifyResult .Success -> moveTo(stateFactory.newSignedInState(result.data, this ::signOut))
578589 }
579590 }
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import aws.smithy.kotlin.runtime.http.HttpException
2020import com.amplifyframework.auth.AuthUserAttributeKey.email
2121import com.amplifyframework.auth.AuthUserAttributeKey.emailVerified
2222import com.amplifyframework.auth.MFAType
23+ import com.amplifyframework.auth.exceptions.SessionExpiredException
2324import com.amplifyframework.auth.exceptions.UnknownException
2425import com.amplifyframework.auth.result.AuthResetPasswordResult
2526import com.amplifyframework.auth.result.step.AuthNextResetPasswordStep
@@ -129,6 +130,21 @@ class AuthenticatorViewModelTest {
129130 viewModel.currentStep shouldBe AuthenticatorStep .Error
130131 }
131132
133+ @Test
134+ fun `getCurrentUser error with session expired exception during start results in SignIn state` () = runTest {
135+ coEvery { authProvider.fetchAuthSession() } returns Success (mockAuthSession(isSignedIn = true ))
136+ coEvery { authProvider.getCurrentUser() } returns AmplifyResult .Error (SessionExpiredException ())
137+
138+ viewModel.start(mockAuthenticatorConfiguration())
139+ advanceUntilIdle()
140+
141+ coVerify(exactly = 1 ) {
142+ authProvider.fetchAuthSession()
143+ authProvider.getCurrentUser()
144+ }
145+ viewModel.currentStep shouldBe AuthenticatorStep .SignIn
146+ }
147+
132148 @Test
133149 fun `when already signed in during start the initial state should be signed in` () = runTest {
134150 coEvery { authProvider.fetchAuthSession() } returns Success (mockAuthSession(isSignedIn = true ))
You can’t perform that action at this time.
0 commit comments