~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
184 lines · 4.8 KB Raw History
<script lang="ts">
	import { onMount, tick } from 'svelte';
	import Button from '#lib/atoms/Button.svelte';
	import SignInForm from '#lib/organisms/SignInForm.svelte';
	import Card from '#lib/templates/Card.svelte';
	import { call, follow, passkeySignIn, signIn, type Credentials } from '#lib/api';
	import { authorizeData, isHinted, passkeyOptions, type Account } from '#lib/data';
	import { hasPasskeyAutofill, hasWebAuthn } from '#lib/webauthn';
	import { alerts, leaveAlert, passkeySignInAlert, signInAlert, words } from '#lib/words';

	let {
		form = $bindable(),
		hinted,
		reached,
		onback,
		onsignedin,
		onfactor
	}: {
		form: Credentials;
		hinted: boolean;
		reached: boolean;
		onback: () => void;
		onsignedin: (account: Account, token?: string) => void;
		onfactor: () => void;
	} = $props();

	let busy = $state<'sign-in' | 'passkey' | 'cancel' | null>(null);
	let alert = $state('');
	let wrong = $state(false);
	let ended = $state(false);
	let passwordField = $state<HTMLInputElement>();

	async function submit() {
		if (busy || ended) return;
		busy = 'sign-in';
		alert = '';
		wrong = false;
		const out = await signIn(form);
		busy = null;
		if (out.kind === 'signed-in') return onsignedin(out.account, out.token);
		if (out.kind === 'factor') return onfactor();
		alert = signInAlert(out.answer, alerts.wrongPassword);
		wrong = out.answer.status === 400;
		if (wrong) {
			await tick();
			passwordField?.focus();
		}
	}

	// What a passkey sign-in answered, the button's or the autofill's.
	function passkeyAnswered(out: Awaited<ReturnType<typeof passkeySignIn>>, quiet: boolean) {
		if (out.kind === 'signed-in') {
			const hint = authorizeData?.loginHint;
			if (hint && !isHinted(out.account, hint)) alert = alerts.otherAccount;
			else onsignedin(out.account, out.token);
		} else if (out.kind === 'no-passkey') {
			if (!quiet) alert = alerts.noPasskeyUsed;
		} else if (out.kind === 'no-options') {
			if (!quiet) alert = alerts.failed;
		} else alert = passkeySignInAlert(out.answer);
	}

	// The identifier field's autofill offers the device's passkeys as the
	// view appears, with the page's own options; picking one signs in. The
	// button asks at once instead, ending the autofill's asking first: a
	// browser asks once at a time.
	let autofill: AbortController | null = null;
	onMount(() => {
		void (async () => {
			if (hinted || !passkeyOptions || !(await hasPasskeyAutofill())) return;
			autofill = new AbortController();
			const signal = autofill.signal;
			const out = await passkeySignIn(() => form.remember, passkeyOptions, signal);
			if (signal.aborted) return;
			autofill = null;
			passkeyAnswered(out, true);
		})();
		return () => autofill?.abort();
	});

	// The whole of a passkey sign-in is busy: the options, the browser
	// asking for a passkey, and `passkey-sign-in`. It sends neither field.
	async function passkey() {
		if (busy || ended) return;
		autofill?.abort();
		autofill = null;
		busy = 'passkey';
		alert = '';
		wrong = false;
		const out = await passkeySignIn(() => form.remember);
		busy = null;
		passkeyAnswered(out, false);
	}

	async function cancel() {
		busy = 'cancel';
		alert = '';
		const a = await call('reject', {});
		if (follow(a)) return;
		busy = null;
		alert = leaveAlert(a);
		ended = a.status === 400;
	}
</script>

<Card heading={words.signIn} {alert}>
	<SignInForm
		id="sign-in"
		bind:identifier={form.identifier}
		bind:password={form.password}
		bind:remember={form.remember}
		bind:passwordField
		{hinted}
		describedby={wrong ? 'alert' : undefined}
		onsubmit={submit}
	/>
	{#snippet actions()}
		<div class="methods">
			<Button
				kind="primary"
				type="submit"
				form="sign-in"
				label={words.signIn}
				wide
				busy={busy === 'sign-in'}
				disabled={busy !== null || ended}
			/>
			{#if hasWebAuthn}
				<p class="or" aria-hidden="true">{words.or}</p>
				<Button
					label={words.signInWithPasskey}
					icon="key"
					wide
					busy={busy === 'passkey'}
					disabled={busy !== null || ended}
					onclick={passkey}
				/>
			{/if}
			<div class="leave">
				{#if reached}
					<Button kind="link" label={words.back} disabled={busy !== null} onclick={onback} />
				{:else}
					<Button
						kind="link"
						label={words.cancel}
						busy={busy === 'cancel'}
						disabled={busy !== null || ended}
						onclick={cancel}
					/>
				{/if}
			</div>
		</div>
	{/snippet}
</Card>

<style>
	.methods {
		display: flex;
		flex-direction: column;
		gap: var(--space-3);
		width: 100%;
	}

	/* "or" between two rules, between the password and every other way in. */
	.or {
		display: flex;
		align-items: center;
		gap: var(--space-3);
		color: var(--mute);
		font-size: var(--text-sm);
	}

	.or::before,
	.or::after {
		content: '';
		flex: 1;
		border-top: var(--border) solid var(--line);
	}

	.leave {
		display: flex;
		justify-content: center;
	}
</style>