diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/auth/Authenticator.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/auth/Authenticator.kt index def8293798..c1aaf11f6f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/auth/Authenticator.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/auth/Authenticator.kt @@ -38,10 +38,15 @@ interface Authenticator { * @param homeServerConnectionConfig this param is used to configure the Homeserver * @param login the login field * @param password the password field + * @param deviceName the initial device name * @param callback the matrix callback on which you'll receive the result of authentication. * @return return a [Cancelable] */ - fun authenticate(homeServerConnectionConfig: HomeServerConnectionConfig, login: String, password: String, callback: MatrixCallback): Cancelable + fun authenticate(homeServerConnectionConfig: HomeServerConnectionConfig, + login: String, + password: String, + deviceName: String, + callback: MatrixCallback): Cancelable /** * Check if there is an authenticated [Session]. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt index 3a6a833dcd..f469b42793 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt @@ -77,10 +77,11 @@ internal class DefaultAuthenticator @Inject constructor(@Unauthenticated override fun authenticate(homeServerConnectionConfig: HomeServerConnectionConfig, login: String, password: String, + deviceName: String, callback: MatrixCallback): Cancelable { val job = GlobalScope.launch(coroutineDispatchers.main) { val sessionOrFailure = runCatching { - authenticate(homeServerConnectionConfig, login, password) + authenticate(homeServerConnectionConfig, login, password, deviceName) } sessionOrFailure.foldToCallback(callback) } @@ -97,12 +98,13 @@ internal class DefaultAuthenticator @Inject constructor(@Unauthenticated private suspend fun authenticate(homeServerConnectionConfig: HomeServerConnectionConfig, login: String, - password: String) = withContext(coroutineDispatchers.io) { + password: String, + deviceName: String) = withContext(coroutineDispatchers.io) { val authAPI = buildAuthAPI(homeServerConnectionConfig) val loginParams = if (Patterns.EMAIL_ADDRESS.matcher(login).matches()) { - PasswordLoginParams.thirdPartyIdentifier(ThreePidMedium.EMAIL, login, password, "Mobile") + PasswordLoginParams.thirdPartyIdentifier(ThreePidMedium.EMAIL, login, password, deviceName) } else { - PasswordLoginParams.userIdentifier(login, password, "Mobile") + PasswordLoginParams.userIdentifier(login, password, deviceName) } val credentials = executeRequest { apiCall = authAPI.login(loginParams) diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginAction.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginAction.kt index 2367b273ea..361314eb24 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginAction.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginAction.kt @@ -23,7 +23,7 @@ sealed class LoginAction : VectorViewModelAction { data class UpdateServerType(val serverType: ServerType) : LoginAction() data class UpdateHomeServer(val homeServerUrl: String) : LoginAction() data class UpdateSignMode(val signMode: SignMode) : LoginAction() - data class Login(val login: String, val password: String) : LoginAction() + data class Login(val login: String, val password: String, val initialDeviceName: String) : LoginAction() data class WebLoginSuccess(val credentials: Credentials) : LoginAction() data class InitWith(val loginConfig: LoginConfig) : LoginAction() data class ResetPassword(val email: String, val newPassword: String) : LoginAction() @@ -31,7 +31,7 @@ sealed class LoginAction : VectorViewModelAction { // Register actions open class RegisterAction : LoginAction() - data class RegisterWith(val username: String, val password: String) : RegisterAction() + data class RegisterWith(val username: String, val password: String, val initialDeviceName: String) : RegisterAction() data class AddEmail(val email: String) : RegisterAction() data class AddMsisdn(val msisdn: String) : RegisterAction() data class ConfirmMsisdn(val code: String) : RegisterAction() diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt index 437730cce6..1a258b6dbe 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt @@ -66,8 +66,8 @@ class LoginFragment @Inject constructor( when (loginViewModel.signMode) { SignMode.Unknown -> error("developer error") - SignMode.SignUp -> loginViewModel.handle(LoginAction.RegisterWith(login, password)) - SignMode.SignIn -> loginViewModel.handle(LoginAction.Login(login, password)) + SignMode.SignUp -> loginViewModel.handle(LoginAction.RegisterWith(login, password, getString(R.string.login_mobile_device))) + SignMode.SignIn -> loginViewModel.handle(LoginAction.Login(login, password, getString(R.string.login_mobile_device))) } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt index 866924036c..d51d140663 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt @@ -42,7 +42,7 @@ import im.vector.riotx.features.session.SessionListener import timber.log.Timber /** - * TODO To speed up registration, consider fetching registration flow instead of login flow at startup + * */ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginViewState, private val authenticator: Authenticator, @@ -113,12 +113,7 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi } private fun handleConfirmMsisdn(action: LoginAction.ConfirmMsisdn) { - setState { - copy( - asyncRegistration = Loading() - ) - } - + setState { copy(asyncRegistration = Loading()) } currentTask = registrationWizard?.confirmMsisdn(action.code, registrationCallback) } @@ -134,7 +129,7 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi setState { copy( - asyncRegistration = Success(data) + asyncRegistration = Uninitialized ) } @@ -155,69 +150,43 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi } private fun handleAddMsisdn(action: LoginAction.AddMsisdn) { - setState { - copy( - asyncRegistration = Loading() - ) - } - + setState { copy(asyncRegistration = Loading()) } currentTask = registrationWizard?.addMsisdn(action.msisdn, registrationCallback) } private fun handleAddEmail(action: LoginAction.AddEmail) { - setState { - copy( - asyncRegistration = Loading() - ) - } - + setState { copy(asyncRegistration = Loading()) } currentTask = registrationWizard?.addEmail(action.email, registrationCallback) } private fun handleAcceptTerms() { - setState { - copy( - asyncRegistration = Loading() - ) - } - + setState { copy(asyncRegistration = Loading()) } currentTask = registrationWizard?.acceptTerms(registrationCallback) } private fun handleRegisterDummy() { - setState { - copy( - asyncRegistration = Loading() - ) - } - + setState { copy(asyncRegistration = Loading()) } currentTask = registrationWizard?.dummy(registrationCallback) } private fun handleRegisterWith(action: LoginAction.RegisterWith) { - setState { - copy( - asyncRegistration = Loading() - ) - } - - currentTask = registrationWizard?.createAccount(action.username, action.password, null /* TODO InitialDisplayName */, object : MatrixCallbackDelegate(registrationCallback) { - override fun onSuccess(data: RegistrationResult) { - isPasswordSent = true - // Not sure that this will work: - // super.onSuccess(data) - registrationCallback.onSuccess(data) - } - }) + setState { copy(asyncRegistration = Loading()) } + currentTask = registrationWizard?.createAccount( + action.username, + action.password, + action.initialDeviceName, + object : MatrixCallbackDelegate(registrationCallback) { + override fun onSuccess(data: RegistrationResult) { + isPasswordSent = true + // Not sure that this will work: + // super.onSuccess(data) + registrationCallback.onSuccess(data) + } + }) } private fun handleCaptchaDone(action: LoginAction.CaptchaDone) { - setState { - copy( - asyncRegistration = Loading() - ) - } - + setState { copy(asyncRegistration = Loading()) } currentTask = registrationWizard?.performReCaptcha(action.captchaResponse, registrationCallback) } @@ -337,20 +306,25 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi ) } - currentTask = authenticator.authenticate(homeServerConnectionConfigFinal, action.login, action.password, object : MatrixCallback { - override fun onSuccess(data: Session) { - onSessionCreated(data) - } + currentTask = authenticator.authenticate( + homeServerConnectionConfigFinal, + action.login, + action.password, + action.initialDeviceName, + object : MatrixCallback { + override fun onSuccess(data: Session) { + onSessionCreated(data) + } - override fun onFailure(failure: Throwable) { - // TODO Handled JobCancellationException - setState { - copy( - asyncLoginAction = Fail(failure) - ) - } - } - }) + override fun onFailure(failure: Throwable) { + // TODO Handled JobCancellationException + setState { + copy( + asyncLoginAction = Fail(failure) + ) + } + } + }) } } @@ -358,9 +332,11 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi val homeServerConnectionConfigFinal = homeServerConnectionConfig if (homeServerConnectionConfigFinal == null) { + // Notify the user + _viewEvents.post(LoginViewEvents.RegistrationError(Throwable("Bad configuration"))) setState { copy( - asyncRegistration = Fail(Throwable("Bad configuration")) + asyncRegistration = Uninitialized ) } } else { diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt index f42d46e22c..e0af466b57 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt @@ -17,13 +17,12 @@ package im.vector.riotx.features.login import com.airbnb.mvrx.* -import im.vector.matrix.android.api.auth.registration.RegistrationResult data class LoginViewState( val asyncLoginAction: Async = Uninitialized, val asyncHomeServerLoginFlowRequest: Async = Uninitialized, val asyncResetPassword: Async = Uninitialized, - val asyncRegistration: Async = Uninitialized + val asyncRegistration: Async = Uninitialized ) : MvRxState { fun isLoading(): Boolean {