Skip to content

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

Update 2FA TOTP display to add space in between digit groups (#1734)

Leendert de Borst8de24260557a
diff --git a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpBlock.tsx b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpBlock.tsx
index 33d4ac1e2..8b1f08d09 100644
--- a/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpBlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/components/Items/Details/TotpBlock.tsx
@@ -7,6 +7,19 @@ import { useDb } from '@/entrypoints/popup/context/DbContext';
import type { TotpCode } from '@/utils/dist/core/models/vault';
+/**
+ * Formats a TOTP code as "XXX XXX" with a space in the middle for better readability.
+ */
+const formatTotpCode = (code: string | undefined): string => {
+ if (!code) {
+ return '';
+ }
+ if (code.length === 6) {
+ return `${code.slice(0, 3)} ${code.slice(3)}`;
+ }
+ return code;
+};
+
type TotpBlockProps = {
itemId: string;
}
@@ -173,7 +186,7 @@ const TotpBlock: React.FC<TotpBlockProps> = ({ itemId }) => {
<div className="flex items-center gap-2">
<div className="flex flex-col items-end">
<span className="text-lg font-bold text-gray-900 dark:text-white">
- {currentCodes[totpCode.Id]}
+ {formatTotpCode(currentCodes[totpCode.Id])}
</span>
<div className="text-xs">
{copiedId === totpCode.Id ? (
diff --git a/apps/mobile-app/components/items/details/TotpSection.tsx b/apps/mobile-app/components/items/details/TotpSection.tsx
index 406dd8eb0..769c5c2d6 100644
--- a/apps/mobile-app/components/items/details/TotpSection.tsx
+++ b/apps/mobile-app/components/items/details/TotpSection.tsx
@@ -8,12 +8,24 @@ import { copyToClipboardWithExpiration } from '@/utils/ClipboardUtility';
import type { Item, TotpCode } from '@/utils/dist/core/models/vault';
import { useColors } from '@/hooks/useColorScheme';
-
import { ThemedText } from '@/components/themed/ThemedText';
import { ThemedView } from '@/components/themed/ThemedView';
import { useDb } from '@/context/DbContext';
import { LocalPreferencesService } from '@/services/LocalPreferencesService';
+/**
+ * Formats a TOTP code as "XXX XXX" with a space in the middle for better readability.
+ */
+const formatTotpCode = (code: string | undefined): string => {
+ if (!code) {
+ return '';
+ }
+ if (code.length === 6) {
+ return `${code.slice(0, 3)} ${code.slice(3)}`;
+ }
+ return code;
+};
+
type TotpSectionProps = {
item: Item;
};
@@ -207,7 +219,7 @@ export const TotpSection: React.FC<TotpSectionProps> = ({ item }) : React.ReactN
{t('items.totpCode')}
</ThemedText>
<ThemedText style={styles.code}>
- {currentCodes[totpCode.Id]}
+ {formatTotpCode(currentCodes[totpCode.Id])}
</ThemedText>
</View>
<View style={styles.timerContainer}>
diff --git a/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor b/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor
index d74faf10f..51c811f67 100644
--- a/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor
+++ b/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpViewer.razor
@@ -140,11 +140,11 @@
}
/// <summary>
- /// Gets the TOTP code for a given secret key from the cached current codes dictionary.
+ /// Gets the raw TOTP code for a given secret key from the cached current codes dictionary.
/// </summary>
/// <param name="secretKey">The secret key to get the code for.</param>
- /// <returns>The TOTP code.</returns>
- private string GetTotpCode(string secretKey)
+ /// <returns>The raw TOTP code without formatting.</returns>
+ private string GetRawTotpCode(string secretKey)
{
if (_currentCodes.TryGetValue(secretKey, out var code))
{
@@ -156,13 +156,38 @@
return newCode;
}
+ /// <summary>
+ /// Gets the formatted TOTP code for a given secret key from the cached current codes dictionary.
+ /// </summary>
+ /// <param name="secretKey">The secret key to get the code for.</param>
+ /// <returns>The formatted TOTP code.</returns>
+ private string GetTotpCode(string secretKey)
+ {
+ return FormatTotpCode(GetRawTotpCode(secretKey));
+ }
+
+ /// <summary>
+ /// Formats a TOTP code as "XXX XXX" with a space in the middle for better readability.
+ /// </summary>
+ /// <param name="code">The raw TOTP code.</param>
+ /// <returns>The formatted TOTP code.</returns>
+ private static string FormatTotpCode(string code)
+ {
+ if (code.Length == 6)
+ {
+ return $"{code[..3]} {code[3..]}";
+ }
+
+ return code;
+ }
+
/// <summary>
/// Copies the TOTP code to the clipboard.
/// </summary>
/// <param name="totpCode">The TOTP code to copy.</param>
private async Task CopyToClipboard(TotpCode totpCode)
{
- var code = GetTotpCode(totpCode.SecretKey);
+ var code = GetRawTotpCode(totpCode.SecretKey);
await JsInteropService.CopyToClipboard(code);
ClipboardCopyService.SetCopied(totpCode.Id.ToString());
StateHasChanged();