moving the reset password new password to the reset confirmation step

- the new password is moved to the in memory view model state
This commit is contained in:
Adam Brown 2022-05-30 11:12:03 +01:00
parent 35163f77ba
commit cc8f17b786
9 changed files with 40 additions and 42 deletions

View file

@ -65,18 +65,14 @@ interface LoginWizard {
* [resetPasswordMailConfirmed] is successfully called.
*
* @param email an email previously associated to the account the user wants the password to be reset.
* @param newPassword the desired new password, can be optionally set here or as part of [resetPasswordMailConfirmed]
*/
suspend fun resetPassword(
email: String,
newPassword: String? = null
)
suspend fun resetPassword(email: String)
/**
* Confirm the new password, once the user has checked their email
* When this method succeed, tha account password will be effectively modified.
*
* @param newPassword the desired new password, required if a password was not supplied to [resetPassword]
* @param newPassword the desired new password
*/
suspend fun resetPasswordMailConfirmed(newPassword: String? = null)
suspend fun resetPasswordMailConfirmed(newPassword: String)
}

View file

@ -104,7 +104,7 @@ internal class DefaultLoginWizard(
return sessionCreator.createSession(credentials, pendingSessionData.homeServerConnectionConfig)
}
override suspend fun resetPassword(email: String, newPassword: String?) {
override suspend fun resetPassword(email: String) {
val param = RegisterAddThreePidTask.Params(
RegisterThreePid.Email(email),
pendingSessionData.clientSecret,
@ -118,18 +118,17 @@ internal class DefaultLoginWizard(
authAPI.resetPassword(AddThreePidRegistrationParams.from(param))
}
pendingSessionData = pendingSessionData.copy(resetPasswordData = ResetPasswordData(newPassword, result))
pendingSessionData = pendingSessionData.copy(resetPasswordData = ResetPasswordData(result))
.also { pendingSessionStore.savePendingSessionData(it) }
}
override suspend fun resetPasswordMailConfirmed(newPassword: String?) {
val param = pendingSessionData.readResetPasswordDataOrThrow(newPassword).let { (response, password) ->
ResetPasswordMailConfirmed.create(
pendingSessionData.clientSecret,
response.sid,
password
)
}
override suspend fun resetPasswordMailConfirmed(newPassword: String) {
val resetPasswordData = pendingSessionData.resetPasswordData ?: throw IllegalStateException("Developer error - Must call resetPassword first")
val param = ResetPasswordMailConfirmed.create(
pendingSessionData.clientSecret,
resetPasswordData.addThreePidRegistrationResponse.sid,
newPassword
)
executeRequest(null) {
authAPI.resetPasswordMailConfirmed(param)
@ -138,14 +137,4 @@ internal class DefaultLoginWizard(
// Set to null?
// resetPasswordData = null
}
private fun PendingSessionData.readResetPasswordDataOrThrow(newPassword: String?): Pair<AddThreePidRegistrationResponse, String> {
return when (resetPasswordData) {
null -> throw IllegalStateException("developer error, no reset password in progress")
else -> {
val password = newPassword ?: resetPasswordData.newPassword ?: throw IllegalStateException("developer error, no new password set")
resetPasswordData.addThreePidRegistrationResponse to password
}
}
}
}

View file

@ -24,6 +24,5 @@ import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistration
*/
@JsonClass(generateAdapter = true)
internal data class ResetPasswordData(
val newPassword: String?,
val addThreePidRegistrationResponse: AddThreePidRegistrationResponse
)

View file

@ -413,7 +413,8 @@ class LoginViewModel @AssistedInject constructor(
copy(
asyncResetPassword = Uninitialized,
asyncResetMailConfirmed = Uninitialized,
resetPasswordEmail = null
resetPasswordEmail = null,
resetPasswordNewPassword = null
)
}
}
@ -488,7 +489,7 @@ class LoginViewModel @AssistedInject constructor(
currentJob = viewModelScope.launch {
try {
safeLoginWizard.resetPassword(action.email, action.newPassword)
safeLoginWizard.resetPassword(action.email)
} catch (failure: Throwable) {
setState {
copy(
@ -501,7 +502,8 @@ class LoginViewModel @AssistedInject constructor(
setState {
copy(
asyncResetPassword = Success(Unit),
resetPasswordEmail = action.email
resetPasswordEmail = action.email,
resetPasswordNewPassword = action.newPassword
)
}
@ -530,7 +532,8 @@ class LoginViewModel @AssistedInject constructor(
currentJob = viewModelScope.launch {
try {
safeLoginWizard.resetPasswordMailConfirmed()
val state = awaitState()
safeLoginWizard.resetPasswordMailConfirmed(state.resetPasswordNewPassword!!)
} catch (failure: Throwable) {
setState {
copy(
@ -542,7 +545,8 @@ class LoginViewModel @AssistedInject constructor(
setState {
copy(
asyncResetMailConfirmed = Success(Unit),
resetPasswordEmail = null
resetPasswordEmail = null,
resetPasswordNewPassword = null
)
}

View file

@ -38,6 +38,8 @@ data class LoginViewState(
@PersistState
val resetPasswordEmail: String? = null,
@PersistState
val resetPasswordNewPassword: String? = null,
@PersistState
val homeServerUrlFromUser: String? = null,
// Can be modified after a Wellknown request

View file

@ -392,7 +392,8 @@ class LoginViewModel2 @AssistedInject constructor(
LoginAction2.ResetResetPassword -> {
setState {
copy(
resetPasswordEmail = null
resetPasswordEmail = null,
resetPasswordNewPassword = null
)
}
}
@ -443,7 +444,7 @@ class LoginViewModel2 @AssistedInject constructor(
currentJob = viewModelScope.launch {
try {
safeLoginWizard.resetPassword(action.email, action.newPassword)
safeLoginWizard.resetPassword(action.email)
} catch (failure: Throwable) {
_viewEvents.post(LoginViewEvents2.Failure(failure))
setState { copy(isLoading = false) }
@ -453,7 +454,8 @@ class LoginViewModel2 @AssistedInject constructor(
setState {
copy(
isLoading = false,
resetPasswordEmail = action.email
resetPasswordEmail = action.email,
resetPasswordNewPassword = action.newPassword
)
}
@ -472,7 +474,8 @@ class LoginViewModel2 @AssistedInject constructor(
currentJob = viewModelScope.launch {
try {
safeLoginWizard.resetPasswordMailConfirmed()
val state = awaitState()
safeLoginWizard.resetPasswordMailConfirmed(state.resetPasswordNewPassword!!)
} catch (failure: Throwable) {
_viewEvents.post(LoginViewEvents2.Failure(failure))
setState { copy(isLoading = false) }
@ -481,7 +484,8 @@ class LoginViewModel2 @AssistedInject constructor(
setState {
copy(
isLoading = false,
resetPasswordEmail = null
resetPasswordEmail = null,
resetPasswordNewPassword = null
)
}

View file

@ -36,6 +36,8 @@ data class LoginViewState2(
@PersistState
val resetPasswordEmail: String? = null,
@PersistState
val resetPasswordNewPassword: String? = null,
@PersistState
val homeServerUrlFromUser: String? = null,
// Can be modified after a Wellknown request

View file

@ -456,7 +456,7 @@ class OnboardingViewModel @AssistedInject constructor(
currentJob = viewModelScope.launch {
try {
safeLoginWizard.resetPassword(action.email, action.newPassword)
safeLoginWizard.resetPassword(action.email)
} catch (failure: Throwable) {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(failure))
@ -466,7 +466,7 @@ class OnboardingViewModel @AssistedInject constructor(
setState {
copy(
isLoading = false,
resetState = ResetState(email = action.email)
resetState = ResetState(email = action.email, newPassword = action.newPassword)
)
}
@ -486,7 +486,8 @@ class OnboardingViewModel @AssistedInject constructor(
currentJob = viewModelScope.launch {
try {
safeLoginWizard.resetPasswordMailConfirmed()
val state = awaitState()
safeLoginWizard.resetPasswordMailConfirmed(state.resetState.newPassword!!)
} catch (failure: Throwable) {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(failure))

View file

@ -86,7 +86,8 @@ data class PersonalizationState(
@Parcelize
data class ResetState(
val email: String? = null
val email: String? = null,
val newPassword: String? = null,
) : Parcelable
@Parcelize