Skip to content

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

Update browser extension translations (#1347)

Leendert de Borst3e7848bb3b11
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Dialogs/MobileUnlockModal.tsx b/apps/browser-extension/src/entrypoints/popup/components/Dialogs/MobileUnlockModal.tsx
index 9272a479b..32ea01646 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Dialogs/MobileUnlockModal.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Dialogs/MobileUnlockModal.tsx
@@ -2,6 +2,7 @@ import QRCode from 'qrcode';
import React, { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
+import { MobileLoginErrorCode } from '@/entrypoints/popup/types/MobileLoginErrorCode';
import { MobileLoginUtility } from '@/entrypoints/popup/utils/MobileLoginUtility';
import type { MobileLoginResult } from '@/utils/types/messaging/MobileLoginResult';
@@ -27,11 +28,25 @@ const MobileUnlockModal: React.FC<IMobileUnlockModalProps> = ({
}) => {
const { t } = useTranslation();
const [qrCodeUrl, setQrCodeUrl] = useState<string | null>(null);
- const [error, setError] = useState<string | null>(null);
+ const [error, setError] = useState<MobileLoginErrorCode | null>(null);
const [timeRemaining, setTimeRemaining] = useState<number>(120); // 2 minutes in seconds
const mobileLoginRef = useRef<MobileLoginUtility | null>(null);
const countdownIntervalRef = useRef<NodeJS.Timeout | null>(null);
+ /**
+ * Get translated error message for error code.
+ */
+ const getErrorMessage = (errorCode: MobileLoginErrorCode): string => {
+ console.log('getErrorMessage', errorCode);
+ switch (errorCode) {
+ case MobileLoginErrorCode.TIMEOUT:
+ return t('auth.errors.mobileLoginRequestExpired');
+ case MobileLoginErrorCode.GENERIC:
+ default:
+ return t('common.errors.unknownError');
+ }
+ };
+
// Countdown timer effect
useEffect(() => {
if (qrCodeUrl && timeRemaining > 0 && isOpen) {
@@ -95,22 +110,24 @@ const MobileUnlockModal: React.FC<IMobileUnlockModalProps> = ({
await onSuccess(result);
// Close modal after successful processing
handleClose();
- } catch (err) {
- // Show error if success handler fails
- setError(err instanceof Error ? err.message : t('common.errors.unknownError'));
+ } catch {
+ // Show error if success handler fails and hide QR code
+ setQrCodeUrl(null);
+ setError(MobileLoginErrorCode.GENERIC);
}
},
- (errorMessage) => {
- setError(errorMessage);
+ (errorCode) => {
+ // Hide QR code when error occurs
+ setQrCodeUrl(null);
+ setError(errorCode);
}
);
} catch (err) {
- // Check if this is a 404 error (endpoint doesn't exist - server version too old for this feature)
- const errorWithStatus = err as Error & { status?: number };
- if (err instanceof Error && errorWithStatus.status === 404) {
- setError(t('common.errors.serverVersionTooOld'));
+ // err is a MobileLoginErrorCode thrown by initiate()
+ if (typeof err === 'string' && Object.values(MobileLoginErrorCode).includes(err as MobileLoginErrorCode)) {
+ setError(err as MobileLoginErrorCode);
} else {
- setError(err instanceof Error ? err.message : t('common.errors.unknownError'));
+ setError(MobileLoginErrorCode.GENERIC);
}
}
};
@@ -192,7 +209,7 @@ const MobileUnlockModal: React.FC<IMobileUnlockModalProps> = ({
{error && (
<div className="mb-4 p-3 bg-red-100 dark:bg-red-900/30 border border-red-400 dark:border-red-700 rounded text-red-700 dark:text-red-400 text-sm">
- {error}
+ {getErrorMessage(error)}
</div>
)}
diff --git a/apps/browser-extension/src/entrypoints/popup/types/MobileLoginErrorCode.ts b/apps/browser-extension/src/entrypoints/popup/types/MobileLoginErrorCode.ts
new file mode 100644
index 000000000..53ad241cf
--- /dev/null
+++ b/apps/browser-extension/src/entrypoints/popup/types/MobileLoginErrorCode.ts
@@ -0,0 +1,15 @@
+/**
+ * Error codes for mobile login operations.
+ * These codes are used to provide translatable error messages to users.
+ */
+export enum MobileLoginErrorCode {
+ /**
+ * The mobile login request has timed out after 2 minutes.
+ */
+ TIMEOUT = 'TIMEOUT',
+
+ /**
+ * A generic error occurred during mobile login.
+ */
+ GENERIC = 'GENERIC',
+}
diff --git a/apps/browser-extension/src/entrypoints/popup/utils/MobileLoginUtility.ts b/apps/browser-extension/src/entrypoints/popup/utils/MobileLoginUtility.ts
index de701ffb3..841715e28 100644
--- a/apps/browser-extension/src/entrypoints/popup/utils/MobileLoginUtility.ts
+++ b/apps/browser-extension/src/entrypoints/popup/utils/MobileLoginUtility.ts
@@ -1,5 +1,7 @@
import { Buffer } from 'buffer';
+import { MobileLoginErrorCode } from '@/entrypoints/popup/types/MobileLoginErrorCode';
+
import type { LoginResponse, MobileLoginInitiateResponse, MobileLoginPollResponse } from '@/utils/dist/shared/models/webapi';
import EncryptionUtility from '@/utils/EncryptionUtility';
import type { MobileLoginResult } from '@/utils/types/messaging/MobileLoginResult';
@@ -25,34 +27,40 @@ export class MobileLoginUtility {
/**
* Initiates a mobile login request and returns the QR code data
+ * @throws {MobileLoginErrorCode} If initiation fails
*/
public async initiate(): Promise<string> {
- // Generate RSA key pair
- const keyPair = await EncryptionUtility.generateRsaKeyPair();
- this.privateKey = keyPair.privateKey;
-
- // Send public key to server (no auth required)
- const response = await this.webApi.rawFetch('auth/mobile-login/initiate', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- clientPublicKey: keyPair.publicKey,
- }),
- });
-
- if (!response.ok) {
- const error = new Error(`Failed to initiate mobile login: ${response.status}`) as Error & { status: number };
- error.status = response.status;
- throw error;
- }
+ try {
+ // Generate RSA key pair
+ const keyPair = await EncryptionUtility.generateRsaKeyPair();
+ this.privateKey = keyPair.privateKey;
+
+ // Send public key to server (no auth required)
+ const response = await this.webApi.rawFetch('auth/mobile-login/initiate', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ clientPublicKey: keyPair.publicKey,
+ }),
+ });
+
+ if (!response.ok) {
+ throw MobileLoginErrorCode.GENERIC;
+ }
- const data = await response.json() as MobileLoginInitiateResponse;
- this.requestId = data.requestId;
+ const data = await response.json() as MobileLoginInitiateResponse;
+ this.requestId = data.requestId;
- // Return QR code data (request ID)
- return this.requestId;
+ // Return QR code data (request ID)
+ return this.requestId;
+ } catch (error) {
+ if (typeof error === 'string' && Object.values(MobileLoginErrorCode).includes(error as MobileLoginErrorCode)) {
+ throw error;
+ }
+ throw MobileLoginErrorCode.GENERIC;
+ }
}
/**
@@ -60,7 +68,7 @@ export class MobileLoginUtility {
*/
public async startPolling(
onSuccess: (result: MobileLoginResult) => void,
- onError: (error: string) => void
+ onError: (errorCode: MobileLoginErrorCode) => void
): Promise<void> {
if (!this.requestId || !this.privateKey) {
throw new Error('Must call initiate() before starting polling');
@@ -84,17 +92,21 @@ export class MobileLoginUtility {
);
if (!response.ok) {
+ console.log('polling failed', response.status);
if (response.status === 404) {
// Request expired or not found
this.stopPolling();
this.privateKey = null;
this.requestId = null;
- onError('Mobile login request expired');
+ console.log('request expired or not found');
+ onError(MobileLoginErrorCode.TIMEOUT);
return;
}
throw new Error(`Polling failed: ${response.status}`);
}
+ console.log('polling successful');
+
const data = await response.json() as MobileLoginPollResponse;
if (data.fulfilled && data.encryptedSymmetricKey) {
@@ -129,7 +141,7 @@ export class MobileLoginUtility {
});
if (!loginResponse.ok) {
- onError('Failed to retrieve encryption settings');
+ onError(MobileLoginErrorCode.GENERIC);
return;
}
@@ -154,7 +166,7 @@ export class MobileLoginUtility {
this.stopPolling();
this.privateKey = null;
this.requestId = null;
- onError(error instanceof Error ? error.message : 'Unknown error occurred');
+ onError(MobileLoginErrorCode.UNKNOWN_ERROR);
}
};
@@ -167,7 +179,7 @@ export class MobileLoginUtility {
this.stopPolling();
this.privateKey = null;
this.requestId = null;
- onError('Mobile login request timed out');
+ onError(MobileLoginErrorCode.REQUEST_TIMEOUT);
}
}, 120000);
}
diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.json
index 328bc8dc3..6a49a1fe6 100644
--- a/apps/browser-extension/src/i18n/locales/en.json
+++ b/apps/browser-extension/src/i18n/locales/en.json
@@ -38,7 +38,8 @@
"wrongPassword": "Incorrect password. Please try again.",
"accountLocked": "Account temporarily locked due to too many failed attempts.",
"networkError": "Network error. Please check your connection and try again.",
- "sessionExpired": "Your session has expired. Please log in again."
+ "sessionExpired": "Your session has expired. Please log in again.",
+ "mobileLoginRequestExpired": "Mobile login request timed out. Please reload the page and try again."
}
},
"menu": {