Skip to content

Modern Docker Management, Designed for Everyone

14.5 KB
Raw Blame
<script lang="ts">
	import { goto, refreshAll } from '$app/navigation';
	import {
		browserSupportsWebAuthn,
		startAuthentication,
		type PublicKeyCredentialRequestOptionsJSON
	} from '@simplewebauthn/browser';
	import { createMutation, useQueryClient } from '@tanstack/svelte-query';
	import { mode } from 'mode-watcher';
	import { onMount, untrack } from 'svelte';

	import { ArcaneButton } from '#lib/components/arcane-button/index.js';
	import AuthAmbient from '#lib/components/auth/auth-ambient.svelte';
	import MFAChallenge from '#lib/components/auth/mfa-challenge.svelte';
	import * as Alert from '#lib/components/ui/alert/index.js';
	import * as Card from '#lib/components/ui/card/index.js';
	import * as InputGroup from '#lib/components/ui/input-group/index.js';
	import { Label } from '#lib/components/ui/label/index.js';
	import { AlertIcon, ApiKeyIcon, LockIcon, UserIcon, GithubIcon, OpenIdIcon } from '#lib/icons/index.js';
	import { m } from '#lib/paraglide/messages.js';
	import { queryKeys } from '#lib/query/query-keys.js';
	import { authService, MFARequiredError } from '#lib/services/auth-service.js';
	import { passkeyService } from '#lib/services/passkey-service.js';
	import userStore from '#lib/stores/user-store.svelte.js';
	import type { AuthenticationResponse, MFAChallenge as MFAChallengeData } from '#lib/types/auth.js';
	import { cn } from '#lib/utils.js';
	import { normalizeAuthenticationError } from '#lib/utils/auth.js';
	import { usesDevelopmentBranding } from '#lib/utils/branding.js';
	import { getApplicationLogo } from '#lib/utils/docker.js';
	import { getEffectiveLandingPage } from '#lib/utils/navigation.js';
	import { resolveLogoColor } from '#lib/utils/theme.svelte.js';

	import type { PageProps } from './$types';

	let { data }: PageProps = $props();

	let error = $state<string | null>(null);
	let username = $state('');
	let password = $state('');
	let mfaChallenge = $state<MFAChallengeData | null>(null);
	let passkeySupported = $state(false);
	const showPasskeyLoginButton = $derived(passkeySupported && data.passkeyLoginAvailable === true);
	const queryClient = useQueryClient();

	const logoColor = $derived(resolveLogoColor(mode.current === 'dark'));
	const animationsEnabled = $derived(userStore.current?.preferences?.animationsEnabled ?? true);
	const developmentBranding = $derived(usesDevelopmentBranding());
	const markUrl = $derived(
		getApplicationLogo(false, logoColor, logoColor, {
			animated: animationsEnabled,
			development: developmentBranding
		})
	);

	const oidcEnabledBySettings = $derived(data.settings?.oidcEnabled === true);
	const showOidcLoginButton = $derived(oidcEnabledBySettings);

	const localAuthEnabledBySettings = $derived(data.settings?.authLocalEnabled !== false);
	const showLocalLoginForm = $derived(localAuthEnabledBySettings);

	const oidcAutoRedirect = $derived(data.settings?.oidcAutoRedirectToProvider === true);
	const shouldAutoRedirect = $derived(oidcAutoRedirect && oidcEnabledBySettings && !data.error && !data.isBackupLogin);

	const oidcProviderName = $derived(data.settings?.oidcProviderName || '');
	const oidcProviderLogoUrl = $derived(data.settings?.oidcProviderLogoUrl || '');
	const oidcButtonLabel = $derived(oidcProviderName || m.common_oidc());

	// Only an explicit redirect is handed to the OIDC flow. Resolving the account
	// landing page here would bake the signed-out default into the OIDC round
	// trip, where it would then outrank the user's saved preference.
	const oidcLoginHref = $derived(data.redirectTo ? `/oidc/login?redirect=${encodeURIComponent(data.redirectTo)}` : '/oidc/login');

	const oidcLoginMutation = createMutation(() => ({
		mutationFn: async () => {
			error = null;
			await goto(oidcLoginHref);
		}
	}));

	const loginMutation = createMutation(() => ({
		mutationFn: () => authService.login({ username, password }),
		onSuccess: async (user) => {
			await userStore.setUser(user);
			await queryClient.invalidateQueries({
				queryKey: queryKeys.auth.all
			});
			const redirectTo = data.redirectTo || getEffectiveLandingPage();
			await goto(redirectTo, { replace: true });
		},
		onError: (err) => {
			if (err instanceof MFARequiredError) {
				mfaChallenge = err.challenge;
				error = null;
				return;
			}
			error = normalizeAuthenticationError(err, m.auth_unexpected_error()).message;
		}
	}));

	const passkeyLoginMutation = createMutation(() => ({
		mutationFn: async (): Promise<AuthenticationResponse> => {
			const challenge = await passkeyService.beginLogin();
			const credential = await startAuthentication({
				optionsJSON: challenge.options as unknown as PublicKeyCredentialRequestOptionsJSON
			});
			return passkeyService.finishLogin(challenge.ceremonyId, credential);
		},
		onSuccess: async (response) => {
			if (response.status === 'mfa_required' && response.mfa) {
				mfaChallenge = response.mfa;
				return;
			}
			await authService.completeAuthentication(response);
			await refreshAll();
			await queryClient.invalidateQueries({
				queryKey: queryKeys.auth.all
			});
			await goto(data.redirectTo || getEffectiveLandingPage(), {
				replace: true
			});
		},
		onError: (err) => {
			if (err instanceof Error && err.name === 'NotAllowedError') {
				error = m.auth_passkey_cancelled();
				return;
			}
			error = normalizeAuthenticationError(err, m.auth_passkey_failed()).message;
		}
	}));

	const isLocalLoading = $derived(loginMutation.isPending);
	const isOidcLoading = $derived(oidcLoginMutation.isPending);
	const isPasskeyLoading = $derived(passkeyLoginMutation.isPending);
	const isAnyLoading = $derived(isLocalLoading || isOidcLoading || isPasskeyLoading);

	onMount(() => {
		passkeySupported = browserSupportsWebAuthn();
	});

	$effect(() => {
		if (shouldAutoRedirect) {
			untrack(handleOidcLogin);
		}
	});

	function handleOidcLogin() {
		oidcLoginMutation.mutate(undefined, {
			onError: () => {
				// Fallback to direct navigation when mutation fails unexpectedly
				void goto(oidcLoginHref);
			}
		});
	}

	function handleLogin(event: Event) {
		event.preventDefault();

		if (!username || !password) {
			error = m.auth_credentials_required();
			return;
		}

		error = null;
		loginMutation.mutate();
	}

	async function completeMFA(response: AuthenticationResponse) {
		await authService.completeAuthentication(response);
		await refreshAll();
		await queryClient.invalidateQueries({ queryKey: queryKeys.auth.all });
		await goto(data.redirectTo || getEffectiveLandingPage(), {
			replace: true
		});
	}

	const showProviderRow = $derived(showOidcLoginButton || showPasskeyLoginButton);
	const showDivider = $derived(showProviderRow && showLocalLoginForm);
</script>

<svelte:head>
	<title>{m.layout_title()}</title>
</svelte:head>

<AuthAmbient />

<div class="relative z-(--arcane-z-raised) flex min-h-dvh items-center justify-center p-6">
	<div class="w-full max-w-sm md:max-w-3xl">
		<Card.Root>
			<div
				class={cn('login-beam pointer-events-none absolute inset-0 z-1 rounded-xl', animationsEnabled && 'login-beam--animated')}
			></div>
			<div class="grid md:grid-cols-2">
				<div class="p-6 md:p-8">
					<div class="mb-7 flex flex-col items-center text-center">
						<img
							class={cn('mb-4 drop-shadow-md drop-shadow-primary/15 md:hidden', developmentBranding ? 'size-14' : 'size-10')}
							src={markUrl}
							alt={m.layout_title()}
						/>
						<h1 class="text-2xl font-semibold tracking-tight">
							{m.welcome_back()}
						</h1>
						<p class="mt-1.5 text-sm text-balance text-muted-foreground">
							{m.auth_login_subtitle()}
						</p>
					</div>

					<div class="space-y-4">
						{#if data.error}
							<Alert.Root variant="destructive">
								<AlertIcon class="size-4" />
								<Alert.Title>{m.auth_login_problem_title()}</Alert.Title>
								<Alert.Description>
									{#if data.errorMessage}
										{data.errorMessage}
									{:else if data.error === 'oidc_invalid_response'}
										{m.auth_oidc_invalid_response()}
									{:else if data.error === 'oidc_misconfigured'}
										{m.auth_oidc_misconfigured()}
									{:else if data.error === 'oidc_userinfo_failed'}
										{m.auth_oidc_userinfo_failed()}
									{:else if data.error === 'oidc_missing_sub'}
										{m.auth_oidc_missing_sub()}
									{:else if data.error === 'oidc_email_collision'}
										{m.auth_oidc_email_collision()}
									{:else if data.error === 'oidc_token_error'}
										{m.auth_oidc_token_error()}
									{:else if data.error === 'user_processing_failed'}
										{m.auth_user_processing_failed()}
									{:else if data.error === 'oidc_init_failed'}
										{m.auth_oidc_init_failed()}
									{:else if data.error === 'oidc_url_generation_failed'}
										{m.auth_oidc_url_generation_failed()}
									{:else if data.error === 'oidc_network_error'}
										{m.auth_oidc_network_error()}
									{:else}
										{m.auth_unexpected_error()}
									{/if}
								</Alert.Description>
							</Alert.Root>
						{/if}

						{#if data.errorMessage && !data.error}
							<Alert.Root
								variant="destructive"
								icon={AlertIcon}
								heading={m.auth_login_problem_title()}
								description={data.errorMessage}
							/>
						{/if}

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

						{#if mfaChallenge}
							<MFAChallenge
								challenge={mfaChallenge}
								onComplete={completeMFA}
								onCancel={() => {
									mfaChallenge = null;
									error = null;
								}}
							/>
						{:else}
							{#if !showLocalLoginForm && !showOidcLoginButton && !showPasskeyLoginButton}
								<Alert.Root
									variant="destructive"
									icon={AlertIcon}
									heading={m.auth_no_login_methods_title()}
									description={m.auth_no_login_methods_description()}
								/>
							{/if}

							{#if showLocalLoginForm}
								<form
									id="login-form"
									name="login"
									action=""
									method="post"
									onsubmit={handleLogin}
									class="space-y-4"
									autocomplete="on"
								>
									<div class="space-y-2">
										<Label for="username" size="xs">{m.common_username()}</Label>
										<InputGroup.Root role={undefined}>
											<InputGroup.Addon role={undefined}>
												<UserIcon />
											</InputGroup.Addon>
											<InputGroup.Input
												id="username"
												name="username"
												type="text"
												autocomplete="username"
												aria-label={m.common_username()}
												required
												bind:value={username}
												placeholder={m.auth_username_placeholder()}
												disabled={isAnyLoading}
											/>
										</InputGroup.Root>
									</div>
									<div class="space-y-2">
										<Label for="password" size="xs">{m.common_password()}</Label>
										<InputGroup.Root role={undefined}>
											<InputGroup.Addon role={undefined}>
												<LockIcon />
											</InputGroup.Addon>
											<InputGroup.Input
												id="password"
												name="password"
												type="password"
												autocomplete="current-password"
												aria-label={m.common_password()}
												required
												bind:value={password}
												placeholder={m.auth_password_placeholder()}
												disabled={isAnyLoading}
											/>
										</InputGroup.Root>
									</div>
									<ArcaneButton
										type="submit"
										action="login"
										loading={isLocalLoading}
										disabled={isAnyLoading}
										hoverEffect="none"
									/>
								</form>
							{/if}

							{#if showDivider}
								<div class="flex items-center gap-3 py-1 text-xs text-muted-foreground">
									<div class="h-px flex-1 bg-border/60"></div>
									<span>{m.auth_or_continue()}</span>
									<div class="h-px flex-1 bg-border/60"></div>
								</div>
							{/if}

							{#if showProviderRow}
								<div class="flex flex-wrap items-center gap-2">
									{#if showOidcLoginButton}
										<ArcaneButton
											action="oidc_login"
											hoverEffect="none"
											class="min-w-0 flex-1"
											onclick={() => handleOidcLogin()}
											loading={isOidcLoading}
											disabled={isAnyLoading}
											icon={null}
											customLabel=""
										>
											{#if oidcProviderLogoUrl}
												<img src={oidcProviderLogoUrl} alt="" class="size-4 object-contain" />
											{:else}
												<OpenIdIcon class="size-4" />
											{/if}
											<span class="truncate">{oidcButtonLabel}</span>
										</ArcaneButton>
									{/if}

									{#if showPasskeyLoginButton}
										<ArcaneButton
											action="login"
											icon={ApiKeyIcon}
											customLabel={m.common_passkey()}
											hoverEffect="none"
											class="min-w-0 flex-1"
											loading={isPasskeyLoading}
											disabled={isAnyLoading}
											onclick={() => {
												error = null;
												passkeyLoginMutation.mutate();
											}}
										/>
									{/if}
								</div>
							{/if}
						{/if}
					</div>
				</div>

				<div class="relative hidden flex-col items-center justify-center gap-4 bg-muted/20 md:flex">
					<img class="size-36 drop-shadow-md drop-shadow-primary/15" src={markUrl} alt={m.layout_title()} />
					{#if data.versionInformation?.displayVersion}
						<span class="font-mono text-3xs tracking-ultrawide text-muted-foreground/60 uppercase"
							>{data.versionInformation.displayVersion}</span
						>
					{/if}
				</div>
			</div>
		</Card.Root>

		<div class="mt-6 flex justify-center">
			<a
				href="https://github.com/getarcaneapp/arcane"
				target="_blank"
				rel="noopener noreferrer"
				class="inline-flex items-center gap-1.5 text-xs text-muted-foreground/70 transition-colors hover:text-foreground"
			>
				<GithubIcon class="size-3.5" />
				{m.common_view_on_github()}
			</a>
		</div>
	</div>
</div>

<style>
	@property --login-beam-angle {
		syntax: '<angle>';
		initial-value: -45deg;
		inherits: false;
	}

	/* A 1px ring masked out of a rotating conic gradient, so the highlight travels around the card edge. */
	.login-beam {
		padding: 1px;
		background: conic-gradient(
			from var(--login-beam-angle),
			transparent 0deg,
			var(--primary) 50deg,
			transparent 100deg,
			transparent 180deg,
			color-mix(in oklab, var(--primary) 60%, transparent) 230deg,
			transparent 280deg
		);
		mask:
			linear-gradient(#000 0 0) content-box exclude,
			linear-gradient(#000 0 0);
	}

	.login-beam--animated {
		animation: login-beam-spin 6s linear infinite;
	}

	@keyframes login-beam-spin {
		to {
			--login-beam-angle: 315deg;
		}
	}

	@media (prefers-reduced-motion: reduce) {
		.login-beam--animated {
			animation: none;
		}
	}
</style>