directing to the combined login after homeserver check

- also removes the subtitle view which is not needed for login
This commit is contained in:
Adam Brown 2022-05-05 15:22:16 +01:00
parent 4b6f74364d
commit b3bbb0329e
8 changed files with 71 additions and 79 deletions

View file

@ -19,7 +19,6 @@ package im.vector.app.features.onboarding
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.andThen import im.vector.app.core.extensions.andThen
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import im.vector.app.features.onboarding.OnboardingAction.LoginOrRegister
import org.matrix.android.sdk.api.MatrixPatterns.getServerName import org.matrix.android.sdk.api.MatrixPatterns.getServerName
import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
@ -33,7 +32,7 @@ class DirectLoginUseCase @Inject constructor(
private val uriFactory: UriFactory private val uriFactory: UriFactory
) { ) {
suspend fun execute(action: LoginOrRegister, homeServerConnectionConfig: HomeServerConnectionConfig?): Result<Session> { suspend fun execute(action: OnboardingAction.LoginDirect, homeServerConnectionConfig: HomeServerConnectionConfig?): Result<Session> {
return fetchWellKnown(action.username, homeServerConnectionConfig) return fetchWellKnown(action.username, homeServerConnectionConfig)
.andThen { wellKnown -> createSessionFor(wellKnown, action, homeServerConnectionConfig) } .andThen { wellKnown -> createSessionFor(wellKnown, action, homeServerConnectionConfig) }
} }
@ -42,13 +41,13 @@ class DirectLoginUseCase @Inject constructor(
authenticationService.getWellKnownData(matrixId, config) authenticationService.getWellKnownData(matrixId, config)
} }
private suspend fun createSessionFor(data: WellknownResult, action: LoginOrRegister, config: HomeServerConnectionConfig?) = when (data) { private suspend fun createSessionFor(data: WellknownResult, action: OnboardingAction.LoginDirect, config: HomeServerConnectionConfig?) = when (data) {
is WellknownResult.Prompt -> loginDirect(action, data, config) is WellknownResult.Prompt -> loginDirect(action, data, config)
is WellknownResult.FailPrompt -> handleFailPrompt(data, action, config) is WellknownResult.FailPrompt -> handleFailPrompt(data, action, config)
else -> onWellKnownError() else -> onWellKnownError()
} }
private suspend fun handleFailPrompt(data: WellknownResult.FailPrompt, action: LoginOrRegister, config: HomeServerConnectionConfig?): Result<Session> { private suspend fun handleFailPrompt(data: WellknownResult.FailPrompt, action: OnboardingAction.LoginDirect, config: HomeServerConnectionConfig?): Result<Session> {
// Relax on IS discovery if homeserver is valid // Relax on IS discovery if homeserver is valid
val isMissingInformationToLogin = data.homeServerUrl == null || data.wellKnown == null val isMissingInformationToLogin = data.homeServerUrl == null || data.wellKnown == null
return when { return when {
@ -57,7 +56,7 @@ class DirectLoginUseCase @Inject constructor(
} }
} }
private suspend fun loginDirect(action: LoginOrRegister, wellKnownPrompt: WellknownResult.Prompt, config: HomeServerConnectionConfig?): Result<Session> { private suspend fun loginDirect(action: OnboardingAction.LoginDirect, wellKnownPrompt: WellknownResult.Prompt, config: HomeServerConnectionConfig?): Result<Session> {
val alteredHomeServerConnectionConfig = config?.updateWith(wellKnownPrompt) ?: fallbackConfig(action, wellKnownPrompt) val alteredHomeServerConnectionConfig = config?.updateWith(wellKnownPrompt) ?: fallbackConfig(action, wellKnownPrompt)
return runCatching { return runCatching {
authenticationService.directAuthentication( authenticationService.directAuthentication(
@ -74,7 +73,7 @@ class DirectLoginUseCase @Inject constructor(
identityServerUri = wellKnownPrompt.identityServerUrl?.let { uriFactory.parse(it) } identityServerUri = wellKnownPrompt.identityServerUrl?.let { uriFactory.parse(it) }
) )
private fun fallbackConfig(action: LoginOrRegister, wellKnownPrompt: WellknownResult.Prompt) = HomeServerConnectionConfig( private fun fallbackConfig(action: OnboardingAction.LoginDirect, wellKnownPrompt: WellknownResult.Prompt) = HomeServerConnectionConfig(
homeServerUri = uriFactory.parse("https://${action.username.getServerName()}"), homeServerUri = uriFactory.parse("https://${action.username.getServerName()}"),
homeServerUriBase = uriFactory.parse(wellKnownPrompt.homeServerUrl), homeServerUriBase = uriFactory.parse(wellKnownPrompt.homeServerUrl),
identityServerUri = wellKnownPrompt.identityServerUrl?.let { uriFactory.parse(it) } identityServerUri = wellKnownPrompt.identityServerUrl?.let { uriFactory.parse(it) }

View file

@ -46,10 +46,9 @@ sealed interface OnboardingAction : VectorViewModelAction {
data class ResetPassword(val email: String, val newPassword: String) : OnboardingAction data class ResetPassword(val email: String, val newPassword: String) : OnboardingAction
object ResetPasswordMailConfirmed : OnboardingAction object ResetPasswordMailConfirmed : OnboardingAction
// Login or Register, depending on the signMode
data class LoginOrRegister(val username: String, val password: String, val initialDeviceName: String) : OnboardingAction
data class Register(val username: String, val password: String, val initialDeviceName: String) : OnboardingAction data class Register(val username: String, val password: String, val initialDeviceName: String) : OnboardingAction
data class Login(val username: String, val password: String, val initialDeviceName: String) : OnboardingAction data class Login(val username: String, val password: String, val initialDeviceName: String) : OnboardingAction
data class LoginDirect(val username: String, val password: String, val initialDeviceName: String) : OnboardingAction
object StopEmailValidationCheck : OnboardingAction object StopEmailValidationCheck : OnboardingAction
data class PostRegisterAction(val registerAction: RegisterAction) : OnboardingAction data class PostRegisterAction(val registerAction: RegisterAction) : OnboardingAction

View file

@ -139,9 +139,9 @@ class OnboardingViewModel @AssistedInject constructor(
is OnboardingAction.UpdateSignMode -> handleUpdateSignMode(action) is OnboardingAction.UpdateSignMode -> handleUpdateSignMode(action)
is OnboardingAction.InitWith -> handleInitWith(action) is OnboardingAction.InitWith -> handleInitWith(action)
is OnboardingAction.HomeServerChange -> withAction(action) { handleHomeserverChange(action) } is OnboardingAction.HomeServerChange -> withAction(action) { handleHomeserverChange(action) }
is OnboardingAction.LoginOrRegister -> handleLoginOrRegister(action).also { lastAction = action }
is OnboardingAction.Register -> handleRegisterWith(action).also { lastAction = action } is OnboardingAction.Register -> handleRegisterWith(action).also { lastAction = action }
is OnboardingAction.Login -> handleLogin(action).also { lastAction = action } is OnboardingAction.Login -> handleLogin(action).also { lastAction = action }
is OnboardingAction.LoginDirect -> handleDirectLogin(action, homeServerConnectionConfig = null).also { lastAction = action }
is OnboardingAction.LoginWithToken -> handleLoginWithToken(action) is OnboardingAction.LoginWithToken -> handleLoginWithToken(action)
is OnboardingAction.WebLoginSuccess -> handleWebLoginSuccess(action) is OnboardingAction.WebLoginSuccess -> handleWebLoginSuccess(action)
is OnboardingAction.ResetPassword -> handleResetPassword(action) is OnboardingAction.ResetPassword -> handleResetPassword(action)
@ -215,7 +215,7 @@ class OnboardingViewModel @AssistedInject constructor(
?.let { it.copy(allowedFingerprints = it.allowedFingerprints + action.fingerprint) } ?.let { it.copy(allowedFingerprints = it.allowedFingerprints + action.fingerprint) }
?.let { startAuthenticationFlow(finalLastAction, it) } ?.let { startAuthenticationFlow(finalLastAction, it) }
} }
is OnboardingAction.LoginOrRegister -> is OnboardingAction.LoginDirect ->
handleDirectLogin( handleDirectLogin(
finalLastAction, finalLastAction,
HomeServerConnectionConfig.Builder() HomeServerConnectionConfig.Builder()
@ -488,16 +488,7 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
private fun handleLoginOrRegister(action: OnboardingAction.LoginOrRegister) = withState { state -> private fun handleDirectLogin(action: OnboardingAction.LoginDirect, homeServerConnectionConfig: HomeServerConnectionConfig?) {
when (state.signMode) {
SignMode.Unknown -> error("Developer error, invalid sign mode")
SignMode.SignIn -> handleLogin(OnboardingAction.Login(action.username, action.password, action.initialDeviceName))
SignMode.SignUp -> handleRegisterWith(OnboardingAction.Register(action.username, action.password, action.initialDeviceName))
SignMode.SignInWithMatrixId -> handleDirectLogin(action, null)
}
}
private fun handleDirectLogin(action: OnboardingAction.LoginOrRegister, homeServerConnectionConfig: HomeServerConnectionConfig?) {
setState { copy(isLoading = true) } setState { copy(isLoading = true) }
currentJob = viewModelScope.launch { currentJob = viewModelScope.launch {
directLoginUseCase.execute(action, homeServerConnectionConfig).fold( directLoginUseCase.execute(action, homeServerConnectionConfig).fold(
@ -654,7 +645,11 @@ class OnboardingViewModel @AssistedInject constructor(
when (trigger) { when (trigger) {
is OnboardingAction.HomeServerChange.EditHomeServer -> { is OnboardingAction.HomeServerChange.EditHomeServer -> {
when (awaitState().onboardingFlow) { when (awaitState().onboardingFlow) {
OnboardingFlow.SignUp -> internalRegisterAction(RegisterAction.StartRegistration) { _ -> OnboardingFlow.SignUp -> internalRegisterAction(RegisterAction.StartRegistration) {
updateServerSelection(config, serverTypeOverride, authResult)
_viewEvents.post(OnboardingViewEvents.OnHomeserverEdited)
}
OnboardingFlow.SignIn -> {
updateServerSelection(config, serverTypeOverride, authResult) updateServerSelection(config, serverTypeOverride, authResult)
_viewEvents.post(OnboardingViewEvents.OnHomeserverEdited) _viewEvents.post(OnboardingViewEvents.OnHomeserverEdited)
} }
@ -667,7 +662,10 @@ class OnboardingViewModel @AssistedInject constructor(
when (awaitState().onboardingFlow) { when (awaitState().onboardingFlow) {
OnboardingFlow.SignIn -> { OnboardingFlow.SignIn -> {
updateSignMode(SignMode.SignIn) updateSignMode(SignMode.SignIn)
_viewEvents.post(OnboardingViewEvents.OnSignModeSelected(SignMode.SignIn)) when (vectorFeatures.isOnboardingCombinedLoginEnabled()) {
true -> _viewEvents.post(OnboardingViewEvents.OpenCombinedLogin)
false -> _viewEvents.post(OnboardingViewEvents.OnSignModeSelected(SignMode.SignIn))
}
} }
OnboardingFlow.SignUp -> { OnboardingFlow.SignUp -> {
updateSignMode(SignMode.SignUp) updateSignMode(SignMode.SignUp)

View file

@ -26,6 +26,7 @@ import androidx.autofill.HintConstants
import androidx.core.text.isDigitsOnly import androidx.core.text.isDigitsOnly
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.airbnb.mvrx.withState
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.hideKeyboard import im.vector.app.core.extensions.hideKeyboard
@ -119,6 +120,7 @@ class FtueAuthLoginFragment @Inject constructor() : AbstractSSOFtueAuthFragment<
} }
private fun submit() { private fun submit() {
withState(viewModel) { state ->
cleanupUi() cleanupUi()
val login = views.loginField.text.toString() val login = views.loginField.text.toString()
@ -152,7 +154,15 @@ class FtueAuthLoginFragment @Inject constructor() : AbstractSSOFtueAuthFragment<
} }
if (error == 0) { if (error == 0) {
viewModel.handle(OnboardingAction.LoginOrRegister(login, password, getString(R.string.login_default_session_public_name))) val initialDeviceName = getString(R.string.login_default_session_public_name)
val action = when (state.signMode) {
SignMode.Unknown -> error("developer error")
SignMode.SignUp -> OnboardingAction.Register(login, password, initialDeviceName)
SignMode.SignIn -> OnboardingAction.Login(login, password, initialDeviceName)
SignMode.SignInWithMatrixId -> OnboardingAction.LoginDirect(login, password, initialDeviceName)
}
viewModel.handle(action)
}
} }
} }

View file

@ -46,24 +46,10 @@
android:gravity="center" android:gravity="center"
android:text="@string/ftue_auth_welcome_back_title" android:text="@string/ftue_auth_welcome_back_title"
android:textColor="?vctr_content_primary" android:textColor="?vctr_content_primary"
app:layout_constraintBottom_toTopOf="@id/createAccountHeaderSubtitle"
app:layout_constraintEnd_toEndOf="@id/createAccountGutterEnd"
app:layout_constraintStart_toStartOf="@id/createAccountGutterStart"
app:layout_constraintTop_toBottomOf="@id/headerSpacing" />
<TextView
android:id="@+id/createAccountHeaderSubtitle"
style="@style/Widget.Vector.TextView.Subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/ftue_auth_create_account_subtitle"
android:textColor="?vctr_content_secondary"
app:layout_constraintBottom_toTopOf="@id/titleContentSpacing" app:layout_constraintBottom_toTopOf="@id/titleContentSpacing"
app:layout_constraintEnd_toEndOf="@id/createAccountGutterEnd" app:layout_constraintEnd_toEndOf="@id/createAccountGutterEnd"
app:layout_constraintStart_toStartOf="@id/createAccountGutterStart" app:layout_constraintStart_toStartOf="@id/createAccountGutterStart"
app:layout_constraintTop_toBottomOf="@id/createAccountHeaderTitle" /> app:layout_constraintTop_toBottomOf="@id/headerSpacing" />
<Space <Space
android:id="@+id/titleContentSpacing" android:id="@+id/titleContentSpacing"
@ -71,7 +57,7 @@
android:layout_height="0dp" android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/chooseYourServerHeader" app:layout_constraintBottom_toTopOf="@id/chooseYourServerHeader"
app:layout_constraintHeight_percent="0.03" app:layout_constraintHeight_percent="0.03"
app:layout_constraintTop_toBottomOf="@id/createAccountHeaderSubtitle" /> app:layout_constraintTop_toBottomOf="@id/createAccountHeaderTitle" />
<TextView <TextView
android:id="@+id/chooseYourServerHeader" android:id="@+id/chooseYourServerHeader"

View file

@ -32,7 +32,7 @@ import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.data.WellKnown import org.matrix.android.sdk.api.auth.data.WellKnown
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
private val A_LOGIN_OR_REGISTER_ACTION = OnboardingAction.LoginOrRegister("@a-user:id.org", "a-password", "a-device-name") private val A_LOGIN_OR_REGISTER_ACTION = OnboardingAction.LoginDirect("@a-user:id.org", "a-password", "a-device-name")
private val A_WELLKNOWN_SUCCESS_RESULT = WellknownResult.Prompt("https://homeserverurl.com", identityServerUrl = null, WellKnown()) private val A_WELLKNOWN_SUCCESS_RESULT = WellknownResult.Prompt("https://homeserverurl.com", identityServerUrl = null, WellKnown())
private val A_WELLKNOWN_FAILED_WITH_CONTENT_RESULT = WellknownResult.FailPrompt("https://homeserverurl.com", WellKnown()) private val A_WELLKNOWN_FAILED_WITH_CONTENT_RESULT = WellknownResult.FailPrompt("https://homeserverurl.com", WellKnown())
private val A_WELLKNOWN_FAILED_WITHOUT_CONTENT_RESULT = WellknownResult.FailPrompt(null, null) private val A_WELLKNOWN_FAILED_WITHOUT_CONTENT_RESULT = WellknownResult.FailPrompt(null, null)

View file

@ -59,7 +59,7 @@ private val A_RESULT_IGNORED_REGISTER_ACTION = RegisterAction.SendAgainThreePid
private val A_HOMESERVER_CAPABILITIES = aHomeServerCapabilities(canChangeDisplayName = true, canChangeAvatar = true) private val A_HOMESERVER_CAPABILITIES = aHomeServerCapabilities(canChangeDisplayName = true, canChangeAvatar = true)
private val AN_IGNORED_FLOW_RESULT = FlowResult(missingStages = emptyList(), completedStages = emptyList()) private val AN_IGNORED_FLOW_RESULT = FlowResult(missingStages = emptyList(), completedStages = emptyList())
private val ANY_CONTINUING_REGISTRATION_RESULT = RegistrationResult.NextStep(AN_IGNORED_FLOW_RESULT) private val ANY_CONTINUING_REGISTRATION_RESULT = RegistrationResult.NextStep(AN_IGNORED_FLOW_RESULT)
private val A_LOGIN_OR_REGISTER_ACTION = OnboardingAction.LoginOrRegister("@a-user:id.org", "a-password", "a-device-name") private val A_DIRECT_LOGIN = OnboardingAction.LoginDirect("@a-user:id.org", "a-password", "a-device-name")
private const val A_HOMESERVER_URL = "https://edited-homeserver.org" private const val A_HOMESERVER_URL = "https://edited-homeserver.org"
private val A_HOMESERVER_CONFIG = HomeServerConnectionConfig(FakeUri().instance) private val A_HOMESERVER_CONFIG = HomeServerConnectionConfig(FakeUri().instance)
private val SELECTED_HOMESERVER_STATE = SelectedHomeserverState(preferredLoginMode = LoginMode.Password) private val SELECTED_HOMESERVER_STATE = SelectedHomeserverState(preferredLoginMode = LoginMode.Password)
@ -142,11 +142,11 @@ class OnboardingViewModelTest {
@Test @Test
fun `given has sign in with matrix id sign mode, when handling login or register action, then logs in directly`() = runTest { fun `given has sign in with matrix id sign mode, when handling login or register action, then logs in directly`() = runTest {
viewModelWith(initialState.copy(signMode = SignMode.SignInWithMatrixId)) viewModelWith(initialState.copy(signMode = SignMode.SignInWithMatrixId))
fakeDirectLoginUseCase.givenSuccessResult(A_LOGIN_OR_REGISTER_ACTION, config = null, result = fakeSession) fakeDirectLoginUseCase.givenSuccessResult(A_DIRECT_LOGIN, config = null, result = fakeSession)
givenInitialisesSession(fakeSession) givenInitialisesSession(fakeSession)
val test = viewModel.test() val test = viewModel.test()
viewModel.handle(A_LOGIN_OR_REGISTER_ACTION) viewModel.handle(A_DIRECT_LOGIN)
test test
.assertStatesChanges( .assertStatesChanges(
@ -161,11 +161,11 @@ class OnboardingViewModelTest {
@Test @Test
fun `given has sign in with matrix id sign mode, when handling login or register action fails, then emits error`() = runTest { fun `given has sign in with matrix id sign mode, when handling login or register action fails, then emits error`() = runTest {
viewModelWith(initialState.copy(signMode = SignMode.SignInWithMatrixId)) viewModelWith(initialState.copy(signMode = SignMode.SignInWithMatrixId))
fakeDirectLoginUseCase.givenFailureResult(A_LOGIN_OR_REGISTER_ACTION, config = null, cause = AN_ERROR) fakeDirectLoginUseCase.givenFailureResult(A_DIRECT_LOGIN, config = null, cause = AN_ERROR)
givenInitialisesSession(fakeSession) givenInitialisesSession(fakeSession)
val test = viewModel.test() val test = viewModel.test()
viewModel.handle(A_LOGIN_OR_REGISTER_ACTION) viewModel.handle(A_DIRECT_LOGIN)
test test
.assertStatesChanges( .assertStatesChanges(

View file

@ -25,11 +25,11 @@ import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
class FakeDirectLoginUseCase { class FakeDirectLoginUseCase {
val instance = mockk<DirectLoginUseCase>() val instance = mockk<DirectLoginUseCase>()
fun givenSuccessResult(action: OnboardingAction.LoginOrRegister, config: HomeServerConnectionConfig?, result: FakeSession) { fun givenSuccessResult(action: OnboardingAction.LoginDirect, config: HomeServerConnectionConfig?, result: FakeSession) {
coEvery { instance.execute(action, config) } returns Result.success(result) coEvery { instance.execute(action, config) } returns Result.success(result)
} }
fun givenFailureResult(action: OnboardingAction.LoginOrRegister, config: HomeServerConnectionConfig?, cause: Throwable) { fun givenFailureResult(action: OnboardingAction.LoginDirect, config: HomeServerConnectionConfig?, cause: Throwable) {
coEvery { instance.execute(action, config) } returns Result.failure(cause) coEvery { instance.execute(action, config) } returns Result.failure(cause)
} }
} }