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/vue-example/src/components/Auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { useAuthStore } from '@/stores/auth.store'
import { storeToRefs } from 'pinia'
import Login from './Login.vue'
import Passkey from './passkey/Passkey.vue'
import Logout from './Logout.vue'

const store = useAuthStore()
Expand All @@ -18,6 +19,10 @@ const { user } = storeToRefs(store)
</template>

<template v-else>
<Login />
<div class="gap flex flex-col">
<Passkey />

<Login />
</div>
</template>
</template>
4 changes: 2 additions & 2 deletions templates/vue-example/src/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Button from '@/components/Button.vue'
const signInWithII = async () => {
await signIn({
internet_identity: {},
});
};
})
}
</script>

<template>
Expand Down
109 changes: 109 additions & 0 deletions templates/vue-example/src/components/passkey/CreatePasskey.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<script setup lang="ts">
import {
type SignProgress,
type SignProgressFn,
signUp,
WebAuthnSignUpProgressStep,
} from '@junobuild/core'
import type { PasskeyProgress } from '@/types/passkey.ts'
import { computed, ref } from 'vue'
import Button from '@/components/Button.vue'
import Progress from '@/components/passkey/Progress.vue'

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

const { progress: wizardProgress, onProgress: wizardOnProgress } = defineProps<{
progress: PasskeyProgress | undefined
onProgress: (progress: PasskeyProgress | undefined) => void
}>()

const progress = computed<ProgressSignUp>(() =>
wizardProgress === undefined
? { state: 'init' }
: 'signUp' in wizardProgress
? { state: 'progress', detail: wizardProgress.signUp }
: 'setup' in wizardProgress
? { state: 'setup' }
: { state: 'hidden' },
)

const inputText = ref('')

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

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

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

throw error
}
}
</script>

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

<Button @click="goToSetup">Create a new passkey</Button>
</template>

<template v-else-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"
v-model="inputText"
/>

<Button @click="doSignUp">Create now</Button>
</template>

<template v-else-if="progress.state === 'progress'">
<Progress>
<span v-if="progress.detail.step === WebAuthnSignUpProgressStep.CreatingUserCredential">
Creating user credential...
</span>
<span
v-else-if="progress.detail.step === WebAuthnSignUpProgressStep.ValidatingUserCredential"
>
Validating user credential...
</span>
<span v-else-if="progress.detail.step === WebAuthnSignUpProgressStep.FinalizingCredential">
Finalizing credential...
</span>
<span v-else-if="progress.detail.step === WebAuthnSignUpProgressStep.Signing">
Signing request...
</span>
<span v-else-if="progress.detail.step === WebAuthnSignUpProgressStep.FinalizingSession">
Finalizing session...
</span>
<span v-else-if="progress.detail.step === WebAuthnSignUpProgressStep.RegisteringUser">
Registering user...
</span>
</Progress>
</template>
</template>
86 changes: 86 additions & 0 deletions templates/vue-example/src/components/passkey/Passkey.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import type { PasskeyProgress } from '@/types/passkey.ts'
import { isWebAuthnAvailable } from '@junobuild/core'
import Button from '@/components/Button.vue'
import Backdrop from '@/components/Backdrop.vue'
import CreatePasskey from '@/components/passkey/CreatePasskey.vue'
import UsePasskey from '@/components/passkey/UsePasskey.vue'

const passkeySupported = ref(true)
const showModal = ref(false)
const progress = ref<PasskeyProgress | undefined>(undefined)

onMounted(async () => {
passkeySupported.value = await isWebAuthnAvailable()
})

const start = () => {
progress.value = undefined
showModal.value = true
}

const close = () => {
showModal.value = false
progress.value = undefined
}

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

<template>
<template v-if="passkeySupported">
<Button @click="start">Continue with Passkey</Button>
</template>

<template v-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)]"
>
<template v-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"
@click="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>
</template>

<div class="mt-4">
<CreatePasskey :progress="progress" :onProgress="onProgress" />

<UsePasskey :progress="progress" :onProgress="onProgress" />
</div>
</div>
</div>

<Backdrop />
</template>
</template>
30 changes: 30 additions & 0 deletions templates/vue-example/src/components/passkey/Progress.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<template>
<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"><slot /></p>
</div>
</template>
69 changes: 69 additions & 0 deletions templates/vue-example/src/components/passkey/UsePasskey.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<script setup lang="ts">
import type { PasskeyProgress } from '@/types/passkey.ts'
import { computed } from 'vue'
import {
signIn,
type SignProgress,
type SignProgressFn,
WebAuthnSignInProgressStep,
} from '@junobuild/core'
import Button from '@/components/Button.vue'
import Progress from '@/components/passkey/Progress.vue'

const { progress: wizardProgress, onProgress: wizardOnProgress } = defineProps<{
progress: PasskeyProgress | undefined
onProgress: (progress: PasskeyProgress | undefined) => void
}>()

const progress = computed<SignProgress<WebAuthnSignInProgressStep> | undefined | null>(() =>
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>

<template>
<template v-if="progress === undefined">
<p class="pt-6">Already got one set-up?</p>

<Button @click="doSignIn">Use your passkey</Button>
</template>

<template v-else-if="progress !== null">
<Progress>
<span v-if="progress.step === WebAuthnSignInProgressStep.RequestingUserCredential">
Requesting user credential...
</span>
<span v-else-if="progress.step === WebAuthnSignInProgressStep.FinalizingCredential">
Finalizing credential...
</span>
<span v-else-if="progress.step === WebAuthnSignInProgressStep.Signing">
Signing request...
</span>
<span v-else-if="progress.step === WebAuthnSignInProgressStep.FinalizingSession">
Finalizing session...
</span>
<span v-else-if="progress.step === WebAuthnSignInProgressStep.RetrievingUser">
Loading user...
</span>
</Progress>
</template>
</template>
19 changes: 19 additions & 0 deletions templates/vue-example/src/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