Skip to content

Commit

Permalink
PM-11387 on new create account with email verification, attempt login… (
Browse files Browse the repository at this point in the history
  • Loading branch information
dseverns-livefront authored Aug 29, 2024
1 parent 3c39d8b commit 17c579b
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun NavGraphBuilder.completeRegistrationDestination(
onNavigateBack: () -> Unit,
onNavigateToPasswordGuidance: () -> Unit,
onNavigateToPreventAccountLockout: () -> Unit,
onNavigateToLogin: (email: String, token: String) -> Unit,
onNavigateToLogin: (email: String, token: String?) -> Unit,
) {
composableWithSlideTransitions(
route = COMPLETE_REGISTRATION_ROUTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun CompleteRegistrationScreen(
onNavigateBack: () -> Unit,
onNavigateToPasswordGuidance: () -> Unit,
onNavigateToPreventAccountLockout: () -> Unit,
onNavigateToLogin: (email: String, token: String) -> Unit,
onNavigateToLogin: (email: String, token: String?) -> Unit,
viewModel: CompleteRegistrationViewModel = hiltViewModel(),
) {
val state by viewModel.stateFlow.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.viewModelScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.LoginResult
import com.x8bit.bitwarden.data.auth.repository.model.PasswordStrengthResult
import com.x8bit.bitwarden.data.auth.repository.model.RegisterResult
import com.x8bit.bitwarden.data.platform.manager.FeatureFlagManager
Expand Down Expand Up @@ -106,18 +107,14 @@ class CompleteRegistrationViewModel @Inject constructor(

override fun handleAction(action: CompleteRegistrationAction) {
when (action) {
is Internal -> handleInternalAction(action)
is ConfirmPasswordInputChange -> handleConfirmPasswordInputChanged(action)
is PasswordHintChange -> handlePasswordHintChanged(action)
is PasswordInputChange -> handlePasswordInputChanged(action)
is BackClick -> handleBackClicked()
is ErrorDialogDismiss -> handleDialogDismiss()
is CheckDataBreachesToggle -> handleCheckDataBreachesToggle(action)
is Internal.ReceiveRegisterResult -> {
handleReceiveRegisterAccountResult(action)
}

ContinueWithBreachedPasswordClick -> handleContinueWithBreachedPasswordClick()
is ReceivePasswordStrengthResult -> handlePasswordStrengthResult(action)
CompleteRegistrationAction.LearnToPreventLockoutClick -> {
handlePreventAccountLockoutClickAction()
}
Expand All @@ -127,10 +124,16 @@ class CompleteRegistrationViewModel @Inject constructor(
}

CompleteRegistrationAction.CallToActionClick -> handleCallToActionClick()
}
}

private fun handleInternalAction(action: Internal) {
when (action) {
is Internal.GeneratedPasswordResult -> handleGeneratedPasswordResult(action)
is ReceivePasswordStrengthResult -> handlePasswordStrengthResult(action)
is Internal.ReceiveRegisterResult -> handleReceiveRegisterAccountResult(action)
is Internal.UpdateOnboardingFeatureState -> handleUpdateOnboardingFeatureState(action)
is Internal.GeneratedPasswordResult -> handleGeneratedPasswordResult(
action,
)
is Internal.ReceiveLoginResult -> handleLoginResult(action)
}
}

Expand Down Expand Up @@ -215,13 +218,19 @@ class CompleteRegistrationViewModel @Inject constructor(
}

is RegisterResult.Success -> {
mutableStateFlow.update { it.copy(dialog = null) }
sendEvent(
CompleteRegistrationEvent.NavigateToLogin(
viewModelScope.launch {
val loginResult = authRepository.login(
email = state.userEmail,
password = state.passwordInput,
captchaToken = registerAccountResult.captchaToken,
),
)
)
sendAction(
Internal.ReceiveLoginResult(
loginResult = loginResult,
captchaToken = registerAccountResult.captchaToken,
),
)
}
}

RegisterResult.DataBreachFound -> {
Expand Down Expand Up @@ -259,16 +268,33 @@ class CompleteRegistrationViewModel @Inject constructor(
}
}

private fun handleLoginResult(action: Internal.ReceiveLoginResult) {
clearDialogState()
sendEvent(
CompleteRegistrationEvent.ShowToast(
message = R.string.account_created_success.asText(),
),
)
// If the login result is Success the state based navigation will take care of it.
// otherwise we need to navigate to the login screen.
if (action.loginResult !is LoginResult.Success) {
sendEvent(
CompleteRegistrationEvent.NavigateToLogin(
email = state.userEmail,
captchaToken = action.captchaToken,
),
)
}
}

private fun handleCheckDataBreachesToggle(action: CheckDataBreachesToggle) {
mutableStateFlow.update {
it.copy(isCheckDataBreachesToggled = action.newState)
}
}

private fun handleDialogDismiss() {
mutableStateFlow.update {
it.copy(dialog = null)
}
clearDialogState()
}

private fun handleBackClicked() {
Expand Down Expand Up @@ -393,6 +419,12 @@ class CompleteRegistrationViewModel @Inject constructor(
}
}
}

private fun clearDialogState() {
mutableStateFlow.update {
it.copy(dialog = null)
}
}
}

/**
Expand Down Expand Up @@ -509,7 +541,7 @@ sealed class CompleteRegistrationEvent {
*/
data class NavigateToLogin(
val email: String,
val captchaToken: String,
val captchaToken: String?,
) : CompleteRegistrationEvent()
}

Expand Down Expand Up @@ -595,5 +627,17 @@ sealed class CompleteRegistrationAction {
* Indicates a generated password has been received.
*/
data class GeneratedPasswordResult(val generatedPassword: String) : Internal()

/**
* Indicates registration was successful and will now attempt to login and unlock the vault.
* @property captchaToken The captcha token to use for login. With the login function this
* is possible to be negative.
*
* @see [AuthRepository.login]
*/
data class ReceiveLoginResult(
val loginResult: LoginResult,
val captchaToken: String?,
) : Internal()
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<string name="yes">Yes</string>
<string name="account">Account</string>
<string name="account_created">Your new account has been created! You may now log in.</string>
<string name="account_created_success">Your new account has been created!</string>
<string name="add_an_item">Add an Item</string>
<string name="app_extension">App extension</string>
<string name="autofill_accessibility_description">Use the Bitwarden accessibility service to auto-fill your logins across apps and the web.</string>
Expand Down
Loading

0 comments on commit 17c579b

Please sign in to comment.