Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Fix password field settings initialization (#1235)

Leendert de Borstf7a8189b86d3
diff --git a/apps/browser-extension/src/entrypoints/popup/components/PasswordField.tsx b/apps/browser-extension/src/entrypoints/popup/components/PasswordField.tsx
index 854c646b3..816dece98 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/PasswordField.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/PasswordField.tsx
@@ -1,6 +1,8 @@
import React, { useState, useEffect, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
+import { useDb } from '@/entrypoints/popup/context/DbContext';
+
import type { PasswordSettings } from '@/utils/dist/shared/models/vault';
import { CreatePasswordGenerator } from '@/utils/dist/shared/password-generator';
@@ -15,7 +17,6 @@ interface IPasswordFieldProps {
error?: string;
showPassword?: boolean;
onShowPasswordChange?: (show: boolean) => void;
- initialSettings: PasswordSettings;
}
/**
@@ -29,13 +30,14 @@ const PasswordField: React.FC<IPasswordFieldProps> = ({
placeholder,
error,
showPassword: controlledShowPassword,
- onShowPasswordChange,
- initialSettings
+ onShowPasswordChange
}) => {
const { t } = useTranslation();
+ const dbContext = useDb();
const [internalShowPassword, setInternalShowPassword] = useState(false);
const [showConfigDialog, setShowConfigDialog] = useState(false);
- const [currentSettings, setCurrentSettings] = useState<PasswordSettings>(initialSettings);
+ const [currentSettings, setCurrentSettings] = useState<PasswordSettings | null>(null);
+ const [isLoaded, setIsLoaded] = useState(false);
// Use controlled or uncontrolled showPassword state
const showPassword = controlledShowPassword !== undefined ? controlledShowPassword : internalShowPassword;
@@ -51,11 +53,24 @@ const PasswordField: React.FC<IPasswordFieldProps> = ({
}
}, [controlledShowPassword, onShowPasswordChange]);
- // Initialize settings only once when component mounts
+ // Load password settings from database
useEffect(() => {
- setCurrentSettings({ ...initialSettings });
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, []); // Only run on mount to avoid resetting user changes
+ /**
+ * Load password settings from the database.
+ */
+ const loadSettings = async (): Promise<void> => {
+ try {
+ if (dbContext.sqliteClient) {
+ const settings = dbContext.sqliteClient.getPasswordSettings();
+ setCurrentSettings(settings);
+ setIsLoaded(true);
+ }
+ } catch (error) {
+ console.error('Error loading password settings:', error);
+ }
+ };
+ void loadSettings();
+ }, [dbContext.sqliteClient]);
const generatePassword = useCallback((settings: PasswordSettings) => {
try {
@@ -69,6 +84,9 @@ const PasswordField: React.FC<IPasswordFieldProps> = ({
}, [onChange, setShowPassword]);
const handleLengthChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
+ if (!currentSettings) {
+ return;
+ }
const length = parseInt(e.target.value, 10);
const newSettings = { ...currentSettings, Length: length };
setCurrentSettings(newSettings);
@@ -78,6 +96,9 @@ const PasswordField: React.FC<IPasswordFieldProps> = ({
}, [currentSettings, generatePassword]);
const handleRegeneratePassword = useCallback(() => {
+ if (!currentSettings) {
+ return;
+ }
generatePassword(currentSettings);
}, [generatePassword, currentSettings]);
@@ -98,6 +119,18 @@ const PasswordField: React.FC<IPasswordFieldProps> = ({
setShowConfigDialog(true);
}, []);
+ // Don't render until settings are loaded
+ if (!currentSettings || !isLoaded) {
+ return (
+ <div className="space-y-2">
+ <label htmlFor={id} className="block text-sm font-medium text-gray-900 dark:text-white">
+ {label}
+ </label>
+ <div className="animate-pulse bg-gray-200 dark:bg-gray-700 h-10 rounded-lg"></div>
+ </div>
+ );
+ }
+
return (
<div className="space-y-2">
{/* Label */}
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/credentials/CredentialAddEdit.tsx b/apps/browser-extension/src/entrypoints/popup/pages/credentials/CredentialAddEdit.tsx
index 111bcd970..7169f9b21 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/credentials/CredentialAddEdit.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/credentials/CredentialAddEdit.tsx
@@ -409,9 +409,6 @@ const CredentialAddEdit: React.FC = () => {
}
}, [setValue, watch]);
- const initialPasswordSettings = useMemo(() => {
- return dbContext.sqliteClient?.getPasswordSettings();
- }, [dbContext.sqliteClient]);
/**
* Handle form submission.
@@ -630,18 +627,15 @@ const CredentialAddEdit: React.FC = () => {
error={errors.Username?.message}
onRegenerate={generateRandomUsername}
/>
- {initialPasswordSettings && (
- <PasswordField
- id="password"
- label={t('common.password')}
- value={watch('Password') ?? ''}
- onChange={(value) => setValue('Password', value)}
- error={errors.Password?.message}
- showPassword={showPassword}
- onShowPasswordChange={setShowPassword}
- initialSettings={initialPasswordSettings}
- />
- )}
+ <PasswordField
+ id="password"
+ label={t('common.password')}
+ value={watch('Password') ?? ''}
+ onChange={(value) => setValue('Password', value)}
+ error={errors.Password?.message}
+ showPassword={showPassword}
+ onShowPasswordChange={setShowPassword}
+ />
</div>
</div>
diff --git a/apps/mobile-app/components/form/AdvancedPasswordField.tsx b/apps/mobile-app/components/form/AdvancedPasswordField.tsx
index 6e5b8b0db..2ca7cf83a 100644
--- a/apps/mobile-app/components/form/AdvancedPasswordField.tsx
+++ b/apps/mobile-app/components/form/AdvancedPasswordField.tsx
@@ -69,17 +69,16 @@ const AdvancedPasswordFieldComponent = forwardRef<AdvancedPasswordFieldRef, Adva
if (dbContext.sqliteClient) {
const settings = await dbContext.sqliteClient.getPasswordSettings();
setCurrentSettings(settings);
- // Only set slider value from settings if we don't have a password value yet
- if (!hasSetInitialLength.current && isNewCredential) {
- setSliderValue(settings.Length);
- }
+ // Always set slider value from loaded settings
+ setSliderValue(settings.Length);
+ hasSetInitialLength.current = true;
}
} catch (error) {
console.error('Error loading password settings:', error);
}
};
loadSettings();
- }, [dbContext.sqliteClient, isNewCredential]);
+ }, [dbContext.sqliteClient]);
useImperativeHandle(ref, () => ({