Refactor to prevent duplicate vault saves on vault creation (#820)

This commit is contained in:
Leendert de Borst
2025-07-07 13:15:59 +02:00
committed by Leendert de Borst
parent 27fad07f92
commit 23378368fb
6 changed files with 24 additions and 20 deletions

View File

@@ -52,18 +52,8 @@
if (success)
{
// After successful registration, persist the current language to vault settings
try
{
var currentLanguage = await LanguageService.GetCurrentLanguageAsync();
await DbService.Settings.SetSettingAsync("AppLanguage", currentLanguage);
}
catch
{
// Ignore errors in language setting, don't fail the registration process
}
NavigationManager.NavigateTo("/");
return;
}
else
{

View File

@@ -1,11 +1,15 @@
@page "/user/setup"
@using AliasVault.Client.Auth.Pages.Setup.Components
@using AliasVault.Client.Shared.Components
@using Microsoft.Extensions.Localization
@inherits AliasVault.Client.Auth.Pages.Base.LoginBase
@layout Auth.Layout.EmptyLayout
@attribute [AllowAnonymous]
<div class="min-h-screen bg-gray-100 dark:bg-gray-900 flex flex-col lg:items-center lg:justify-center">
<div class="absolute top-4 right-4 z-10 mt-16 lg:mt-0">
<LanguageSwitcher />
</div>
<div class="w-full mx-auto lg:max-w-xl lg:bg-white lg:dark:bg-gray-800 lg:shadow-xl lg:rounded-lg lg:overflow-hidden flex flex-col">
<div class="flex flex-col flex-grow">
<div class="flex-grow p-6 pt-4 lg:pt-6 pb-28 lg:pb-4">

View File

@@ -26,6 +26,7 @@
@code {
private IStringLocalizer Localizer => LocalizerFactory.Create("Pages.Main.Sync.StatusMessages.Creating", "AliasVault.Client");
private string ErrorMessage { get; set; } = string.Empty;
private bool IsMigrating { get; set; } = false;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
@@ -56,16 +57,17 @@
// Migrate (create) the database.
if (await DbService.CreateNewVaultAsync())
{
// Initialize vault settings with user's browser language preference
await InitializeVaultSettings();
// Save the database to the server.
if (await DbService.SaveDatabaseAsync())
try
{
// Initialize vault settings with user's browser language preference. This also automatically
// saves the new empty database to the server.
await InitializeVaultSettings();
// Migration successful, update state which will trigger the status message UI to refresh and redirect user.
DbService.GetState().UpdateState(DbServiceState.DatabaseStatus.Ready);
return;
}
else
catch
{
// Migration failed
ErrorMessage = Localizer["SaveFailedError"];
@@ -87,6 +89,7 @@
{
try
{
Console.WriteLine("Initializing vault settings with user preferences detected from the browser.");
// Get the current browser language preference
var browserLanguage = await LanguageService.GetBrowserLanguageAsync();

View File

@@ -329,6 +329,10 @@ public sealed class DbService : IDisposable
{
await _dbContext.Database.ExecuteSqlRawAsync(sqlCommand);
}
// Init settings service.
_isSuccessfullyInitialized = true;
await _settingsService.InitializeAsync(this);
}
catch (Exception ex)
{

View File

@@ -330,6 +330,12 @@ public sealed class SettingsService
/// <returns>Task.</returns>
private async Task SetSettingAsync(string key, string value)
{
// Only update if the value has changed.
if (_settings.GetValueOrDefault(key) == value)
{
return;
}
var db = await _dbService!.GetDbContextAsync();
var setting = await db.Settings.FindAsync(key);
if (setting == null)

View File

@@ -403,9 +403,6 @@ public class ClientPlaywrightTest : PlaywrightTest
if (checkForSuccess)
{
await WaitForUrlAsync("welcome**", WelcomeMessage);
// Wait for 100ms as without this the welcome redirect sometimes is not fully awaited and is triggered again.
await Task.Delay(100);
}
}
}