Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ internal object SessionHelper {
)
}
}

internal fun CognitoUserPoolTokens.isValid() = SessionHelper.isValidTokens(this)
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ internal class AuthUseCaseFactory(
)

fun getCurrentUser() = GetCurrentUserUseCase(
fetchAuthSession = fetchAuthSession(),
stateMachine = stateMachine
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,12 @@ package com.amplifyframework.auth.cognito.usecases
import com.amplifyframework.auth.AuthUser
import com.amplifyframework.auth.cognito.AuthStateMachine
import com.amplifyframework.auth.cognito.requireSignedInState
import com.amplifyframework.auth.exceptions.SessionExpiredException
import com.amplifyframework.util.throwIf

internal class GetCurrentUserUseCase(
private val fetchAuthSession: FetchAuthSessionUseCase,
private val stateMachine: AuthStateMachine
) {
suspend fun execute(): AuthUser {
val state = stateMachine.requireSignedInState()

// Throw exception if session is expired
val result = fetchAuthSession.execute().userPoolTokensResult
result.error.throwIf<SessionExpiredException>()

return AuthUser(
state.signedInData.userId,
state.signedInData.username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.amplifyframework.auth.cognito.usecases
import com.amplifyframework.auth.cognito.AuthStateMachine
import com.amplifyframework.auth.cognito.mockSignedInData
import com.amplifyframework.auth.exceptions.InvalidStateException
import com.amplifyframework.auth.exceptions.SessionExpiredException
import com.amplifyframework.auth.exceptions.SignedOutException
import com.amplifyframework.statemachine.codegen.states.AuthenticationState
import io.kotest.assertions.throwables.shouldThrow
Expand All @@ -29,10 +28,6 @@ import kotlinx.coroutines.test.runTest
import org.junit.Test

class GetCurrentUserUseCaseTest {
private val fetchAuthSession: FetchAuthSessionUseCase = mockk {
coEvery { execute().userPoolTokensResult.error } returns null
}

private val stateMachine: AuthStateMachine = mockk {
coEvery { getCurrentState().authNState } returns AuthenticationState.SignedIn(
signedInData = mockSignedInData(username = "username", userId = "sub"),
Expand All @@ -41,7 +36,6 @@ class GetCurrentUserUseCaseTest {
}

private val useCase = GetCurrentUserUseCase(
fetchAuthSession = fetchAuthSession,
stateMachine = stateMachine
)

Expand All @@ -68,13 +62,4 @@ class GetCurrentUserUseCaseTest {
useCase.execute()
}
}

@Test
fun `throws exception for expired session`() = runTest {
val error = SessionExpiredException()
coEvery { fetchAuthSession.execute().userPoolTokensResult.error } returns error
shouldThrow<SessionExpiredException> {
useCase.execute()
} shouldBe error
}
}