Skip to content

Modern Docker Management, Designed for Everyone

4.1 KB
Raw Blame
<script lang="ts">
	import { startAuthentication, type PublicKeyCredentialRequestOptionsJSON } from '@simplewebauthn/browser';

	import { ArcaneButton } from '#lib/components/arcane-button/index.js';
	import * as Alert from '#lib/components/ui/alert/index.js';
	import { Button } from '#lib/components/ui/button/index.js';
	import * as InputGroup from '#lib/components/ui/input-group/index.js';
	import { Label } from '#lib/components/ui/label/index.js';
	import { ApiKeyIcon, AlertIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { passkeyService } from '#lib/services/passkey-service.js';
	import type { AuthenticationResponse, MFAChallenge } from '#lib/types/auth.js';
	import { normalizeAuthenticationError } from '#lib/utils/auth.js';
	import { tryCatch } from '#lib/utils/try-catch.js';

	let {
		challenge,
		onComplete,
		onCancel
	}: {
		challenge: MFAChallenge;
		onComplete: (response: AuthenticationResponse) => Promise<void>;
		onCancel?: () => void;
	} = $props();

	let recoveryCode = $state('');
	let busy = $state(false);
	let error = $state<string | null>(null);

	function isCancelledError(value: unknown): boolean {
		return value instanceof Error && value.name === 'NotAllowedError';
	}

	async function completeWithPasskey() {
		busy = true;
		error = null;
		try {
			const operationResult1 = await tryCatch(
				(async () => {
					const credential = await startAuthentication({
						optionsJSON: $state.snapshot(challenge.options) as unknown as PublicKeyCredentialRequestOptionsJSON
					});
					const response = await passkeyService.finishMFA(challenge.transactionId, credential);
					await onComplete(response);
				})()
			);
			if (operationResult1.error !== null) {
				const value = operationResult1.error;

				if (!isCancelledError(value)) {
					error = normalizeAuthenticationError(value, m.auth_mfa_failed()).message;
				}
			}
		} finally {
			busy = false;
		}
	}

	async function completeWithRecoveryCode(event: SubmitEvent) {
		event.preventDefault();
		if (!recoveryCode.trim()) return;

		busy = true;
		error = null;
		try {
			const operationResult2 = await tryCatch(
				(async () => {
					const response = await passkeyService.finishRecovery(challenge.transactionId, recoveryCode.trim());
					await onComplete(response);
				})()
			);
			if (operationResult2.error !== null) {
				const value = operationResult2.error;

				error = normalizeAuthenticationError(value, m.auth_mfa_failed()).message;
			}
		} finally {
			busy = false;
		}
	}
</script>

<div class="space-y-5 rounded-2xl border bg-card/60 p-5 shadow-sm ring-1 ring-border/40">
	<div class="space-y-2">
		<div class="flex items-center gap-2">
			<ApiKeyIcon class="size-5 text-primary" />
			<h2 class="text-lg font-semibold">{m.auth_mfa_title()}</h2>
		</div>
		<p class="text-sm text-muted-foreground">{m.auth_mfa_description()}</p>
	</div>

	{#if error}
		<Alert.Root variant="destructive" icon={AlertIcon} heading={m.auth_failed_title()} description={error} />
	{/if}

	<ArcaneButton
		action="login"
		icon={ApiKeyIcon}
		customLabel={m.auth_mfa_use_passkey()}
		hoverEffect="none"
		loading={busy}
		disabled={busy}
		onclick={() => void completeWithPasskey()}
	/>

	<div class="relative flex items-center">
		<div class="w-full border-t border-border/60"></div>
		<span class="absolute left-1/2 -translate-x-1/2 rounded-full border bg-card px-3 py-1 text-xs text-muted-foreground">
			{m.auth_or_continue()}
		</span>
	</div>

	<form class="space-y-3" onsubmit={completeWithRecoveryCode}>
		<Label for="mfa-recovery-code">{m.auth_mfa_recovery_code()}</Label>
		<InputGroup.Root role={undefined}>
			<InputGroup.Input
				id="mfa-recovery-code"
				name="recoveryCode"
				autocomplete="one-time-code"
				placeholder={m.auth_mfa_recovery_code_placeholder()}
				bind:value={recoveryCode}
				disabled={busy}
			/>
		</InputGroup.Root>
		<Button type="submit" variant="outline" class="w-full" disabled={busy || !recoveryCode.trim()}>
			{m.auth_mfa_use_recovery_code()}
		</Button>
	</form>

	{#if onCancel}
		<Button type="button" variant="ghost" class="w-full" disabled={busy} onclick={() => onCancel?.()}>
			{m.auth_mfa_cancel()}
		</Button>
	{/if}
</div>