-
Notifications
You must be signed in to change notification settings - Fork 91
/
WebAuthnModal.vue
131 lines (125 loc) · 2.56 KB
/
WebAuthnModal.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<script setup lang="ts">
const show = ref(false)
const logging = ref(false)
const userName = ref('')
const displayName = ref('')
const company = ref('')
const toast = useToast()
const { user, fetch } = useUserSession()
const { register, authenticate, isSupported } = useWebAuthn()
async function signUp() {
if (logging.value || !userName.value) return
logging.value = true
await register({
userName: userName.value,
displayName: displayName.value,
company: company.value,
})
.then(() => {
fetch()
show.value = false
})
.catch((err) => {
console.log(err)
toast.add({
color: 'red',
title: err.data?.message || err.message,
})
})
logging.value = false
}
async function signIn() {
if (logging.value) return
logging.value = true
await authenticate(userName.value)
.then(() => {
fetch()
show.value = false
})
.catch((err) => {
console.log(err)
toast.add({
color: 'red',
title: err.data?.message || err.message,
})
})
logging.value = false
}
</script>
<template>
<UButton
v-if="!user?.webauthn && isSupported"
size="xs"
color="gray"
@click="show = true"
>
Passkeys
</UButton>
<UDashboardModal
v-model="show"
title="Login with credential"
description="First time? Register your credential"
>
<form
class="space-y-4"
@submit.prevent="signUp"
>
<UFormGroup
label="Email"
required
>
<UInput
v-model="userName"
name="email"
type="email"
/>
</UFormGroup>
<UFormGroup label="Name">
<UInput
v-model="displayName"
name="name"
type="text"
/>
</UFormGroup>
<UFormGroup label="Company">
<UInput
v-model="company"
name="name"
type="text"
/>
</UFormGroup>
<UButton
type="submit"
:disabled="!userName"
color="black"
class="mt-2"
>
Register
</UButton>
</form>
<UDivider label="Or" />
<form
class="space-y-4"
@submit.prevent="signIn"
>
<UFormGroup
label="Email"
required
>
<UInput
v-model="userName"
name="email"
type="email"
autocomplete="username webauthn"
/>
</UFormGroup>
<UButton
type="submit"
color="black"
class="mt-2"
>
Authenticate
</UButton>
</form>
</UDashboardModal>
</template>