This commit is contained in:
jarek committed 2026-01-01 16:00:34 +01:00
1 parent 5475112806
commit 242f8df49d
35 files changed
+1892 -1987

No files matched your search

+12 -20
View File
@@ -26,7 +26,6 @@
} from 'lucide-svelte';
import { authStore } from '$lib/stores/auth';
import * as Alert from '$lib/components/ui/alert';
import { licenseStore } from '$lib/stores/license';
import AvatarCropper from '$lib/components/AvatarCropper.svelte';
import * as Avatar from '$lib/components/ui/avatar';
import ChangePasswordModal from './ChangePasswordModal.svelte';
@@ -542,26 +541,19 @@
</p>
</div>
</div>
{#if $licenseStore.isEnterprise}
{#if profile.mfaEnabled}
<Button variant="outline" onclick={() => showDisableMfaModal = true}>
Disable MFA
</Button>
{:else}
<Button onclick={setupMfa} disabled={mfaLoading}>
{#if mfaLoading}
<RefreshCw class="w-4 h-4 mr-1 animate-spin" />
{:else}
<QrCode class="w-4 h-4 mr-1" />
{/if}
Setup MFA
</Button>
{/if}
{#if profile.mfaEnabled}
<Button variant="outline" onclick={() => showDisableMfaModal = true}>
Disable MFA
</Button>
{:else}
<Badge variant="outline" class="gap-1 rounded-sm">
<Crown class="w-3 h-3" />
Enterprise
</Badge>
<Button onclick={setupMfa} disabled={mfaLoading}>
{#if mfaLoading}
<RefreshCw class="w-4 h-4 mr-1 animate-spin" />
{:else}
<QrCode class="w-4 h-4 mr-1" />
{/if}
Setup MFA
</Button>
{/if}
</div>
{:else}
+134 -45
View File
@@ -3,7 +3,7 @@
import * as Dialog from '$lib/components/ui/dialog';
import { Label } from '$lib/components/ui/label';
import { Input } from '$lib/components/ui/input';
import { QrCode, RefreshCw, ShieldCheck, TriangleAlert } from 'lucide-svelte';
import { QrCode, RefreshCw, ShieldCheck, TriangleAlert, Copy, Download, Check } from 'lucide-svelte';
import * as Alert from '$lib/components/ui/alert';
import { focusFirstInput } from '$lib/utils';
@@ -21,10 +21,16 @@
let token = $state('');
let loading = $state(false);
let error = $state('');
let backupCodes = $state<string[]>([]);
let showBackupCodes = $state(false);
let copied = $state(false);
function resetForm() {
token = '';
error = '';
backupCodes = [];
showBackupCodes = false;
copied = false;
}
async function verifyAndEnableMfa() {
@@ -43,11 +49,12 @@
body: JSON.stringify({ action: 'verify', token })
});
const data = await response.json();
if (response.ok) {
onSuccess();
onClose();
backupCodes = data.backupCodes || [];
showBackupCodes = true;
} else {
const data = await response.json();
error = data.error || 'Invalid verification code';
}
} catch (e) {
@@ -57,61 +64,143 @@
}
}
function formatBackupCodes(): string {
return backupCodes.map((code, i) => `${i + 1}. ${code}`).join('\n');
}
async function copyBackupCodes() {
try {
await navigator.clipboard.writeText(formatBackupCodes());
copied = true;
setTimeout(() => copied = false, 2000);
} catch {
error = 'Failed to copy to clipboard';
}
}
function downloadBackupCodes() {
const content = `Dockhand MFA Backup Codes\n${'='.repeat(30)}\n\nThese codes can be used to sign in if you lose access to your authenticator app.\nEach code can only be used once.\n\n${formatBackupCodes()}\n\nGenerated: ${new Date().toISOString()}`;
const blob = new Blob([content], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'dockhand-backup-codes.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function handleDone() {
onSuccess();
onClose();
}
</script>
<Dialog.Root bind:open onOpenChange={(o) => { if (o) { resetForm(); focusFirstInput(); } else onClose(); }}>
<Dialog.Root bind:open onOpenChange={(o) => { if (o) { resetForm(); focusFirstInput(); } else if (!showBackupCodes) onClose(); }}>
<Dialog.Content class="max-w-md">
<Dialog.Header>
<Dialog.Title class="flex items-center gap-2">
<QrCode class="w-5 h-5" />
Setup two-factor authentication
{#if showBackupCodes}
<ShieldCheck class="w-5 h-5 text-green-500" />
MFA enabled successfully
{:else}
<QrCode class="w-5 h-5" />
Setup two-factor authentication
{/if}
</Dialog.Title>
</Dialog.Header>
<div class="space-y-4">
{#if error}
<Alert.Root variant="destructive">
{#if showBackupCodes}
<!-- Backup codes view -->
<div class="space-y-4">
<Alert.Root>
<TriangleAlert class="h-4 w-4" />
<Alert.Description>{error}</Alert.Description>
<Alert.Description>
Save these backup codes in a safe place. Each code can only be used once to sign in if you lose access to your authenticator app.
</Alert.Description>
</Alert.Root>
{/if}
<p class="text-sm text-muted-foreground">
Scan this QR code with your authenticator app (Google Authenticator, Authy, etc.)
</p>
{#if qrCode}
<div class="flex justify-center p-4 bg-white rounded-lg">
<img src={qrCode} alt="MFA QR Code" class="w-48 h-48" />
<div class="grid grid-cols-2 gap-2 p-3 bg-muted rounded-lg font-mono text-sm">
{#each backupCodes as code, i}
<div class="flex items-center gap-2">
<span class="text-muted-foreground w-4">{i + 1}.</span>
<span>{code}</span>
</div>
{/each}
</div>
{/if}
<div class="space-y-2">
<Label class="text-xs text-muted-foreground">Or enter this code manually:</Label>
<code class="block p-2 bg-muted rounded text-sm font-mono break-all">{secret}</code>
<div class="flex gap-2">
<Button variant="outline" class="flex-1" onclick={copyBackupCodes}>
{#if copied}
<Check class="w-4 h-4 mr-1" />
Copied!
{:else}
<Copy class="w-4 h-4 mr-1" />
Copy codes
{/if}
</Button>
<Button variant="outline" class="flex-1" onclick={downloadBackupCodes}>
<Download class="w-4 h-4 mr-1" />
Download
</Button>
</div>
</div>
<div class="space-y-2">
<Label>Verification code</Label>
<Input
bind:value={token}
placeholder="Enter 6-digit code"
maxlength={6}
/>
<p class="text-xs text-muted-foreground">
Enter the code from your authenticator app to verify setup
</p>
</div>
</div>
<Dialog.Footer>
<Button variant="outline" onclick={onClose}>Cancel</Button>
<Button onclick={verifyAndEnableMfa} disabled={loading || !token}>
{#if loading}
<RefreshCw class="w-4 h-4 mr-1 animate-spin" />
{:else}
<Dialog.Footer>
<Button onclick={handleDone}>
<ShieldCheck class="w-4 h-4 mr-1" />
Done
</Button>
</Dialog.Footer>
{:else}
<!-- Setup view -->
<div class="space-y-4">
{#if error}
<Alert.Root variant="destructive">
<TriangleAlert class="h-4 w-4" />
<Alert.Description>{error}</Alert.Description>
</Alert.Root>
{/if}
Enable MFA
</Button>
</Dialog.Footer>
<p class="text-sm text-muted-foreground">
Scan this QR code with your authenticator app (Google Authenticator, Authy, etc.)
</p>
{#if qrCode}
<div class="flex justify-center p-4 bg-white rounded-lg">
<img src={qrCode} alt="MFA QR Code" class="w-48 h-48" />
</div>
{/if}
<div class="space-y-2">
<Label class="text-xs text-muted-foreground">Or enter this code manually:</Label>
<code class="block p-2 bg-muted rounded text-sm font-mono break-all">{secret}</code>
</div>
<div class="space-y-2">
<Label>Verification code</Label>
<Input
bind:value={token}
placeholder="Enter 6-digit code"
maxlength={6}
/>
<p class="text-xs text-muted-foreground">
Enter the code from your authenticator app to verify setup
</p>
</div>
</div>
<Dialog.Footer>
<Button variant="outline" onclick={onClose}>Cancel</Button>
<Button onclick={verifyAndEnableMfa} disabled={loading || !token}>
{#if loading}
<RefreshCw class="w-4 h-4 mr-1 animate-spin" />
{:else}
<ShieldCheck class="w-4 h-4 mr-1" />
{/if}
Enable MFA
</Button>
</Dialog.Footer>
{/if}
</Dialog.Content>
</Dialog.Root>