Skip to content

Commit

Permalink
fix(authenticator): fix signed in state when access token is expired (#…
Browse files Browse the repository at this point in the history
…170)

Co-authored-by: Matt Creaser <[email protected]>
  • Loading branch information
phantumcode and mattcreaser authored Aug 16, 2024
1 parent 888460d commit 77fbc8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.amplifyframework.auth.cognito.exceptions.service.UserNotConfirmedExce
import com.amplifyframework.auth.cognito.exceptions.service.UserNotFoundException
import com.amplifyframework.auth.cognito.exceptions.service.UsernameExistsException
import com.amplifyframework.auth.exceptions.NotAuthorizedException
import com.amplifyframework.auth.exceptions.SessionExpiredException
import com.amplifyframework.auth.exceptions.UnknownException
import com.amplifyframework.auth.options.AuthSignUpOptions
import com.amplifyframework.auth.result.AuthResetPasswordResult
Expand Down Expand Up @@ -573,7 +574,17 @@ internal class AuthenticatorViewModel(
private suspend fun handleSignedIn() {
logger.debug("Log in successful, getting current user")
when (val result = authProvider.getCurrentUser()) {
is AmplifyResult.Error -> handleGeneralFailure(result.error)
is AmplifyResult.Error -> {
if (result.error is SessionExpiredException) {
logger.error(result.error.toString())
logger.error("Current signed in user session has expired, signing out.")
signOut()
moveTo(AuthenticatorStep.SignIn)
} else {
handleGeneralFailure(result.error)
}
}

is AmplifyResult.Success -> moveTo(stateFactory.newSignedInState(result.data, this::signOut))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import aws.smithy.kotlin.runtime.http.HttpException
import com.amplifyframework.auth.AuthUserAttributeKey.email
import com.amplifyframework.auth.AuthUserAttributeKey.emailVerified
import com.amplifyframework.auth.MFAType
import com.amplifyframework.auth.exceptions.SessionExpiredException
import com.amplifyframework.auth.exceptions.UnknownException
import com.amplifyframework.auth.result.AuthResetPasswordResult
import com.amplifyframework.auth.result.step.AuthNextResetPasswordStep
Expand Down Expand Up @@ -129,6 +130,21 @@ class AuthenticatorViewModelTest {
viewModel.currentStep shouldBe AuthenticatorStep.Error
}

@Test
fun `getCurrentUser error with session expired exception during start results in SignIn state`() = runTest {
coEvery { authProvider.fetchAuthSession() } returns Success(mockAuthSession(isSignedIn = true))
coEvery { authProvider.getCurrentUser() } returns AmplifyResult.Error(SessionExpiredException())

viewModel.start(mockAuthenticatorConfiguration())
advanceUntilIdle()

coVerify(exactly = 1) {
authProvider.fetchAuthSession()
authProvider.getCurrentUser()
}
viewModel.currentStep shouldBe AuthenticatorStep.SignIn
}

@Test
fun `when already signed in during start the initial state should be signed in`() = runTest {
coEvery { authProvider.fetchAuthSession() } returns Success(mockAuthSession(isSignedIn = true))
Expand Down

0 comments on commit 77fbc8a

Please sign in to comment.