Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion templates/sveltekit-example/src/lib/components/Auth.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { userSignedIn } from '$lib/derived/user.derived';
import Logout from '$lib/components/Logout.svelte';
import Login from '$lib/components/Login.svelte';
import Passkey from '$lib/components/passkey/Passkey.svelte';

interface Props {
children: Snippet;
Expand All @@ -30,5 +31,9 @@
<Logout />
</div>
{:else}
<Login />
<div class="gap flex flex-col">
<Passkey />

<Login />
</div>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const signInWithII = async () => {
await signIn({
internet_identity: {},
internet_identity: {}
});
};
</script>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<script lang="ts">
import {
type SignProgress,
type SignProgressFn,
signUp,
WebAuthnSignUpProgressStep
} from '@junobuild/core';
import type { PasskeyProps } from '$lib/types/passkey';
import Button from '$lib/components/Button.svelte';
import Progress from '$lib/components/passkey/Progress.svelte';

type ProgressSignUp =
| { state: 'init' | 'setup' | 'hidden' }
| { state: 'progress'; detail: SignProgress<WebAuthnSignUpProgressStep> };

let { progress: wizardProgress, onProgress: wizardOnProgress }: PasskeyProps = $props();

let inputText = $state('');

const progress = $derived(
wizardProgress === undefined
? { state: 'init' }
: 'signUp' in wizardProgress
? { state: 'progress', detail: wizardProgress.signUp }
: 'setup' in wizardProgress
? { state: 'setup' }
: { state: 'hidden' }
);

const goToSetup = async () => {
wizardOnProgress({ setup: null });
};

const onProgress: SignProgressFn<WebAuthnSignUpProgressStep> = (progress) =>
wizardOnProgress({ signUp: progress });

const doSignUp = async () => {
try {
await signUp({
webauthn: {
options: {
onProgress,
...(inputText !== '' && {
passkey: { user: { displayName: inputText } }
})
}
}
});
} catch (error: unknown) {
wizardOnProgress(undefined);

throw error;
}
};
</script>

{#if progress.state === 'init'}
<p>First time here? Use your device (Face ID, Windows Hello, or screen lock) to get in.</p>

<Button onclick={goToSetup}>Create a new passkey</Button>
{/if}

{#if progress.state === 'setup'}
<p>Want to give it a nickname so you'll spot it easily later?</p>

<input
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"
placeholder="An optional nickname"
bind:value={inputText}
/>

<Button onclick={doSignUp}>Create now</Button>
{/if}

{#if progress.state === 'progress'}
<Progress>
{#if progress.detail.step === WebAuthnSignUpProgressStep.CreatingUserCredential}
<span>Creating user credential...</span>
{:else if progress.detail.step === WebAuthnSignUpProgressStep.ValidatingUserCredential}
<span>Validating user credential...</span>
{:else if progress.detail.step === WebAuthnSignUpProgressStep.FinalizingCredential}
<span>Finalizing credential...</span>
{:else if progress.detail.step === WebAuthnSignUpProgressStep.Signing}
<span>Signing request...</span>
{:else if progress.detail.step === WebAuthnSignUpProgressStep.FinalizingSession}
<span>Finalizing session...</span>
{:else if progress.detail.step === WebAuthnSignUpProgressStep.RegisteringUser}
<span>Registering user...</span>
{/if}
</Progress>
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<script lang="ts">
import { isWebAuthnAvailable } from '@junobuild/core';
import type { PasskeyProgress } from '$lib/types/passkey';
import { onMount } from 'svelte';
import Button from '$lib/components/Button.svelte';
import CreatePasskey from '$lib/components/passkey/CreatePasskey.svelte';
import UsePasskey from '$lib/components/passkey/UsePasskey.svelte';
import Backdrop from '$lib/components/Backdrop.svelte';

let passkeySupported = $state(true);
let showModal = $state(false);
let progress = $state<PasskeyProgress | undefined>(undefined);

onMount(async () => {
passkeySupported = await isWebAuthnAvailable();
});

const start = async () => {
progress = undefined;
showModal = true;
};

const close = async () => {
showModal = false;
progress = undefined;
};

const onProgress = (p: PasskeyProgress | undefined) => {
progress = p;
};
</script>

{#if passkeySupported}
<Button onclick={start}>Continue with Passkey</Button>
{/if}

{#if showModal}
<div
class="animate-fade fixed inset-0 z-50 p-16 md:px-24 md:py-44"
role="dialog"
aria-modal="true"
aria-labelledby="modalTitle"
>
<div
class="w-full max-w-md rounded-sm border-[3px] border-black bg-white px-4 py-3 shadow-[5px_5px_0px_rgba(0,0,0,1)]"
>
{#if progress === undefined || 'setup' in progress}
<div class="flex items-start justify-between">
<h2 id="modalTitle" class="text-xl font-bold text-gray-900 sm:text-2xl">Hey 👋</h2>

<button
type="button"
class="-me-4 -mt-4 rounded-full p-2 text-gray-400 transition-colors hover:bg-gray-50 hover:text-gray-600 focus:outline-none"
aria-label="Close"
onclick={close}
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="size-5"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
{/if}

<div class="mt-4">
<CreatePasskey {progress} {onProgress} />
<UsePasskey {progress} {onProgress} />
</div>
</div>
</div>

<Backdrop />
{/if}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts">
import type { Snippet } from 'svelte';

interface Props {
children: Snippet;
}

let { children }: Props = $props();
</script>

<div class="flex flex-col items-center justify-center gap-2 pt-6">
<svg
class="animate-spin"
viewBox="0 0 64 64"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width="48"
height="48"
>
<path
d="M32 3C35.8083 3 39.5794 3.75011 43.0978 5.20749C46.6163 6.66488 49.8132 8.80101 52.5061 11.4939C55.199 14.1868 57.3351 17.3837 58.7925 20.9022C60.2499 24.4206 61 28.1917 61 32C61 35.8083 60.2499 39.5794 58.7925 43.0978C57.3351 46.6163 55.199 49.8132 52.5061 52.5061C49.8132 55.199 46.6163 57.3351 43.0978 58.7925C39.5794 60.2499 35.8083 61 32 61C28.1917 61 24.4206 60.2499 20.9022 58.7925C17.3837 57.3351 14.1868 55.199 11.4939 52.5061C8.801 49.8132 6.66487 46.6163 5.20749 43.0978C3.7501 39.5794 3 35.8083 3 32C3 28.1917 3.75011 24.4206 5.2075 20.9022C6.66489 17.3837 8.80101 14.1868 11.4939 11.4939C14.1868 8.80099 17.3838 6.66487 20.9022 5.20749C24.4206 3.7501 28.1917 3 32 3L32 3Z"
stroke="currentColor"
stroke-width="5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M32 3C36.5778 3 41.0906 4.08374 45.1692 6.16256C49.2477 8.24138 52.7762 11.2562 55.466 14.9605C58.1558 18.6647 59.9304 22.9531 60.6448 27.4748C61.3591 31.9965 60.9928 36.6232 59.5759 40.9762"
stroke="currentColor"
stroke-width="5"
stroke-linecap="round"
stroke-linejoin="round"
class="text-lavender-blue-500"
></path>
</svg>

<p class="max-w-1/2 pb-6 text-center">{@render children()}</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<script lang="ts">
import {
signIn,
type SignProgress,
type SignProgressFn,
WebAuthnSignInProgressStep
} from '@junobuild/core';
import type { PasskeyProps } from '$lib/types/passkey';
import Button from '$lib/components/Button.svelte';
import Progress from '$lib/components/passkey/Progress.svelte';

let { progress: wizardProgress, onProgress: wizardOnProgress }: PasskeyProps = $props();

const progress = $derived(
wizardProgress === undefined
? undefined
: 'signIn' in wizardProgress
? wizardProgress.signIn
: null
);

const onProgress: SignProgressFn<WebAuthnSignInProgressStep> = (p) => {
wizardOnProgress({ signIn: p });
};

const doSignIn = async () => {
try {
await signIn({
webauthn: { options: { onProgress } }
});
} catch (error: unknown) {
wizardOnProgress(undefined);

throw error;
}
};
</script>

{#if progress === undefined}
<p class="pt-6">Already got one set-up?</p>

<Button onclick={doSignIn}>Use your passkey</Button>
{:else if progress !== null}
<Progress>
{#if progress.step === WebAuthnSignInProgressStep.RequestingUserCredential}
<span>Requesting user credential...</span>
{:else if progress.step === WebAuthnSignInProgressStep.FinalizingCredential}
<span>Finalizing credential...</span>
{:else if progress.step === WebAuthnSignInProgressStep.Signing}
<span>Signing request...</span>
{:else if progress.step === WebAuthnSignInProgressStep.FinalizingSession}
<span>Finalizing session...</span>
{:else if progress.step === WebAuthnSignInProgressStep.RetrievingUser}
<span>Loading user...</span>
{/if}
</Progress>
{/if}
19 changes: 19 additions & 0 deletions templates/sveltekit-example/src/lib/types/passkey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
WebAuthnSignInProgressStep,
type SignProgress,
WebAuthnSignUpProgressStep
} from '@junobuild/core';

export type PasskeyProgress =
| {
signUp: SignProgress<WebAuthnSignUpProgressStep>;
}
| {
signIn: SignProgress<WebAuthnSignInProgressStep>;
}
| { setup: null };

export interface PasskeyProps {
progress: PasskeyProgress | undefined;
onProgress: (progress: PasskeyProgress | undefined) => void;
}
Loading