|
| 1 | +<script lang="ts"> |
| 2 | + import { |
| 3 | + type SignProgress, |
| 4 | + type SignProgressFn, |
| 5 | + signUp, |
| 6 | + WebAuthnSignUpProgressStep |
| 7 | + } from '@junobuild/core'; |
| 8 | + import type { PasskeyProps } from '$lib/types/passkey'; |
| 9 | + import Button from '$lib/components/Button.svelte'; |
| 10 | + import Progress from '$lib/components/passkey/Progress.svelte'; |
| 11 | +
|
| 12 | + type ProgressSignUp = |
| 13 | + | { state: 'init' | 'setup' | 'hidden' } |
| 14 | + | { state: 'progress'; detail: SignProgress<WebAuthnSignUpProgressStep> }; |
| 15 | +
|
| 16 | + let { progress: wizardProgress, onProgress: wizardOnProgress }: PasskeyProps = $props(); |
| 17 | +
|
| 18 | + let inputText = $state(''); |
| 19 | +
|
| 20 | + const progress = $derived( |
| 21 | + wizardProgress === undefined |
| 22 | + ? { state: 'init' } |
| 23 | + : 'signUp' in wizardProgress |
| 24 | + ? { state: 'progress', detail: wizardProgress.signUp } |
| 25 | + : 'setup' in wizardProgress |
| 26 | + ? { state: 'setup' } |
| 27 | + : { state: 'hidden' } |
| 28 | + ); |
| 29 | +
|
| 30 | + const goToSetup = async () => { |
| 31 | + wizardOnProgress({ setup: null }); |
| 32 | + }; |
| 33 | +
|
| 34 | + const onProgress: SignProgressFn<WebAuthnSignUpProgressStep> = (progress) => |
| 35 | + wizardOnProgress({ signUp: progress }); |
| 36 | +
|
| 37 | + const doSignUp = async () => { |
| 38 | + try { |
| 39 | + await signUp({ |
| 40 | + webauthn: { |
| 41 | + options: { |
| 42 | + onProgress, |
| 43 | + ...(inputText !== '' && { |
| 44 | + passkey: { user: { displayName: inputText } } |
| 45 | + }) |
| 46 | + } |
| 47 | + } |
| 48 | + }); |
| 49 | + } catch (error: unknown) { |
| 50 | + wizardOnProgress(undefined); |
| 51 | +
|
| 52 | + throw error; |
| 53 | + } |
| 54 | + }; |
| 55 | +</script> |
| 56 | + |
| 57 | +{#if progress.state === 'init'} |
| 58 | + <p>First time here? Use your device (Face ID, Windows Hello, or screen lock) to get in.</p> |
| 59 | + |
| 60 | + <Button onclick={goToSetup}>Create a new passkey</Button> |
| 61 | +{/if} |
| 62 | + |
| 63 | +{#if progress.state === 'setup'} |
| 64 | + <p>Want to give it a nickname so you'll spot it easily later?</p> |
| 65 | + |
| 66 | + <input |
| 67 | + class="mx-0 mt-2 mb-6 block w-full resize-none rounded-sm border-[3px] border-black bg-white px-3 py-1.5 text-base font-normal shadow-[5px_5px_0px_rgba(0,0,0,1)] focus:outline-hidden" |
| 68 | + placeholder="An optional nickname" |
| 69 | + bind:value={inputText} |
| 70 | + /> |
| 71 | + |
| 72 | + <Button onclick={doSignUp}>Create now</Button> |
| 73 | +{/if} |
| 74 | + |
| 75 | +{#if progress.state === 'progress'} |
| 76 | + <Progress> |
| 77 | + {#if progress.detail.step === WebAuthnSignUpProgressStep.CreatingUserCredential} |
| 78 | + <span>Creating user credential...</span> |
| 79 | + {:else if progress.detail.step === WebAuthnSignUpProgressStep.ValidatingUserCredential} |
| 80 | + <span>Validating user credential...</span> |
| 81 | + {:else if progress.detail.step === WebAuthnSignUpProgressStep.FinalizingCredential} |
| 82 | + <span>Finalizing credential...</span> |
| 83 | + {:else if progress.detail.step === WebAuthnSignUpProgressStep.Signing} |
| 84 | + <span>Signing request...</span> |
| 85 | + {:else if progress.detail.step === WebAuthnSignUpProgressStep.FinalizingSession} |
| 86 | + <span>Finalizing session...</span> |
| 87 | + {:else if progress.detail.step === WebAuthnSignUpProgressStep.RegisteringUser} |
| 88 | + <span>Registering user...</span> |
| 89 | + {/if} |
| 90 | + </Progress> |
| 91 | +{/if} |
0 commit comments