-
-
Notifications
You must be signed in to change notification settings - Fork 641
Expand file tree
/
Copy pathPasskeys.vue
More file actions
169 lines (147 loc) · 4.79 KB
/
Copy pathPasskeys.vue
File metadata and controls
169 lines (147 loc) · 4.79 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<script setup>
import { computed, ref, watch } from 'vue';
import { startRegistration, browserSupportsWebAuthn } from '@simplewebauthn/browser';
import { router } from '@inertiajs/vue3';
import axios from 'axios'
import Head from '@/pages/layout/Head.vue';
import { Header, Button, EmptyStateMenu, EmptyStateItem, Listing, DropdownItem, Modal, ConfirmationModal, Input, ModalClose, Field } from '@ui';
import { toggleArchitecturalBackground } from '@/pages/layout/architectural-background.js';
const props = defineProps([
'passkeys',
'createUrl',
'storeUrl',
'deleteUrl',
])
watch(
() => props.passkeys,
(passkeys) => toggleArchitecturalBackground(passkeys.length === 0),
{ immediate: true }
);
const error = ref(null);
const showCreateModal = ref(false);
const showErrorModal = computed(() => !!error.value);
const passkeyWaiting = ref(false);
const passkeyName = ref('');
watch(showCreateModal, (opened) => {
if (!opened) passkeyName.value = '';
});
const columns = [
{ label: __('Name'), field: 'name' },
{ label: __('Last Login'), field: 'last_login' },
];
function deletePasskey(passkey) {
if (confirm(__('Are you sure?'))) {
axios.delete(passkey.delete_url).then(() => router.reload());
}
}
async function createPasskey() {
if (! browserSupportsWebAuthn()) {
alert(__('statamic::messages.passkeys_browser_unsupported'));
return;
}
passkeyWaiting.value = true;
const name = passkeyName.value || `${__('Passkey')} ${props.passkeys.length + 1}`;
showCreateModal.value = false;
const authOptionsResponse = await fetch(props.createUrl);
let startRegistrationResponse;
try {
startRegistrationResponse = await startRegistration({ optionsJSON: await authOptionsResponse.json() });
} catch (e) {
console.error(e);
passkeyWaiting.value = false;
return;
}
axios.post(props.storeUrl, { ...startRegistrationResponse, name })
.then(response => {
if (response && response.data.verified) {
router.reload();
return;
}
error.value = response.data.message;
}).catch(e => handleAxiosError(e))
.finally(() => passkeyWaiting.value = false);
}
function handleAxiosError(e) {
if (e.response) {
const { message, errors } = e.response.data;
error.value = message;
return;
}
error.value = __('Something went wrong');
}
</script>
<template>
<Head :title="__('Passkeys')" />
<Header :title="__('Passkeys')" icon="key">
<template #actions>
<Button
variant="primary"
:text="__('Create Passkey')"
@click="showCreateModal = true"
:disabled="passkeyWaiting"
:loading="passkeyWaiting"
/>
</template>
</Header>
<Listing
v-if="passkeys.length"
:items="passkeys"
:columns
:allow-search="false"
:allow-customizing-columns="false"
>
<template #cell-last_login="{ value }">
<date-time v-if="value" :of="value" :options="{ relative: true }" />
<template v-else>{{ __('Never') }}</template>
</template>
<template #prepended-row-actions="{ row: passkey }">
<DropdownItem
@click="deletePasskey(passkey)"
:text="__('Delete')"
icon="trash"
variant="destructive"
/>
</template>
</Listing>
<EmptyStateMenu v-else :heading="__('statamic::messages.passkeys_configure_intro')">
<EmptyStateItem
@click="showCreateModal = true"
icon="key"
:heading="__('Create a Passkey')"
:description="__('Get started by creating your first passkey.')"
/>
</EmptyStateMenu>
<ConfirmationModal
:open="showErrorModal"
:title="__('There was an error creating your passkey')"
:body-text="error"
:cancellable="false"
:button-text="__('OK')"
blur
@update:open="error = null"
/>
<Modal
:title="__('Create a Passkey')"
v-model:open="showCreateModal"
blur
>
<Field :label="__('Name')">
<Input
v-model="passkeyName"
@keyup.enter="createPasskey"
/>
</Field>
<template #footer>
<div class="flex items-center justify-end space-x-3 pt-3 pb-1">
<ModalClose>
<Button variant="ghost" :text="__('Cancel')" />
</ModalClose>
<Button
variant="primary"
:text="__('Create')"
@click="createPasskey"
/>
</div>
</template>
</Modal>
</template>