mirror of
https://github.com/element-hq/element-android
synced 2024-12-18 07:12:47 +03:00
Login screens: set initial device name
This commit is contained in:
parent
ac377fceba
commit
20ad3abb60
6 changed files with 59 additions and 77 deletions
|
@ -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<Session>): Cancelable
|
||||
fun authenticate(homeServerConnectionConfig: HomeServerConnectionConfig,
|
||||
login: String,
|
||||
password: String,
|
||||
deviceName: String,
|
||||
callback: MatrixCallback<Session>): Cancelable
|
||||
|
||||
/**
|
||||
* Check if there is an authenticated [Session].
|
||||
|
|
|
@ -77,10 +77,11 @@ internal class DefaultAuthenticator @Inject constructor(@Unauthenticated
|
|||
override fun authenticate(homeServerConnectionConfig: HomeServerConnectionConfig,
|
||||
login: String,
|
||||
password: String,
|
||||
deviceName: String,
|
||||
callback: MatrixCallback<Session>): 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<Credentials> {
|
||||
apiCall = authAPI.login(loginParams)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,53 +150,32 @@ 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<RegistrationResult>(registrationCallback) {
|
||||
setState { copy(asyncRegistration = Loading()) }
|
||||
currentTask = registrationWizard?.createAccount(
|
||||
action.username,
|
||||
action.password,
|
||||
action.initialDeviceName,
|
||||
object : MatrixCallbackDelegate<RegistrationResult>(registrationCallback) {
|
||||
override fun onSuccess(data: RegistrationResult) {
|
||||
isPasswordSent = true
|
||||
// Not sure that this will work:
|
||||
|
@ -212,12 +186,7 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi
|
|||
}
|
||||
|
||||
private fun handleCaptchaDone(action: LoginAction.CaptchaDone) {
|
||||
setState {
|
||||
copy(
|
||||
asyncRegistration = Loading()
|
||||
)
|
||||
}
|
||||
|
||||
setState { copy(asyncRegistration = Loading()) }
|
||||
currentTask = registrationWizard?.performReCaptcha(action.captchaResponse, registrationCallback)
|
||||
}
|
||||
|
||||
|
@ -337,7 +306,12 @@ class LoginViewModel @AssistedInject constructor(@Assisted initialState: LoginVi
|
|||
)
|
||||
}
|
||||
|
||||
currentTask = authenticator.authenticate(homeServerConnectionConfigFinal, action.login, action.password, object : MatrixCallback<Session> {
|
||||
currentTask = authenticator.authenticate(
|
||||
homeServerConnectionConfigFinal,
|
||||
action.login,
|
||||
action.password,
|
||||
action.initialDeviceName,
|
||||
object : MatrixCallback<Session> {
|
||||
override fun onSuccess(data: Session) {
|
||||
onSessionCreated(data)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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<Unit> = Uninitialized,
|
||||
val asyncHomeServerLoginFlowRequest: Async<LoginMode> = Uninitialized,
|
||||
val asyncResetPassword: Async<Unit> = Uninitialized,
|
||||
val asyncRegistration: Async<RegistrationResult> = Uninitialized
|
||||
val asyncRegistration: Async<Unit> = Uninitialized
|
||||
) : MvRxState {
|
||||
|
||||
fun isLoading(): Boolean {
|
||||
|
|
Loading…
Reference in a new issue