fix: refactor encryption utilities to safely check availability

Moved isEncryptionAvailable() to top of file and refactored to prevent
"Cannot read properties of undefined" errors when safeStorage exists
but doesn't have the isEncryptionAvailable method.

Changes:
- Move isEncryptionAvailable() before other functions so it can be used
- Add typeof check for isEncryptionAvailable method existence
- Use isEncryptionAvailable() helper in encryptApiKey() instead of
  directly calling safeStorage.isEncryptionAvailable()
- Ensures consistent safe checking across all encryption operations

This fixes the settings UI crash that prevented API key management
section from rendering.
This commit is contained in:
2025-10-25 23:55:34 -04:00
parent f2a12ff3c2
commit efd1ff306e

View File

@@ -7,6 +7,16 @@ try {
console.warn('Electron safeStorage not available, API keys will be stored in plaintext');
}
/**
* Checks if encryption is available on the current platform
* @returns true if safeStorage encryption is available
*/
export function isEncryptionAvailable(): boolean {
return safeStorage !== null &&
typeof safeStorage.isEncryptionAvailable === 'function' &&
safeStorage.isEncryptionAvailable();
}
/**
* Encrypts an API key using Electron's safeStorage API
* Falls back to plaintext if encryption is not available (e.g., Linux without keyring)
@@ -19,7 +29,7 @@ export function encryptApiKey(apiKey: string): string {
}
// Check if safeStorage is available and encryption is enabled
if (!safeStorage || !safeStorage.isEncryptionAvailable()) {
if (!isEncryptionAvailable()) {
console.warn('Encryption not available, storing API key in plaintext');
return apiKey;
}
@@ -64,13 +74,3 @@ export function decryptApiKey(stored: string): string {
throw new Error('Failed to decrypt API key. You may need to regenerate it.');
}
}
/**
* Checks if encryption is available on the current platform
* @returns true if safeStorage encryption is available
*/
export function isEncryptionAvailable(): boolean {
return safeStorage !== null &&
typeof safeStorage.isEncryptionAvailable === 'function' &&
safeStorage.isEncryptionAvailable();
}