WebauthnKeys.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div>
  3. <div class="mt-6">
  4. <h3 class="font-bold text-xl">Device Authentication (WebAuthn)</h3>
  5. <div class="my-4 w-24 border-b-2 border-grey-200"></div>
  6. <p class="my-6">
  7. Hardware security keys you have registered for 2nd factor authentication. To remove a key
  8. simply click the delete button next to it. Disabling all keys will turn off 2FA on your
  9. account.
  10. </p>
  11. <div>
  12. <p class="mb-0" v-if="keys.length === 0">You have not registered any hardware keys.</p>
  13. <div class="table w-full text-sm md:text-base" v-if="keys.length > 0">
  14. <div class="table-row">
  15. <div class="table-cell p-1 md:p-4 font-semibold">Name</div>
  16. <div class="table-cell p-1 md:p-4 font-semibold">Created</div>
  17. <div class="table-cell p-1 md:p-4 font-semibold">Enabled</div>
  18. <div class="table-cell p-1 md:p-4 text-right">
  19. <a href="/webauthn/keys/create" class="text-indigo-700">Add New Key</a>
  20. </div>
  21. </div>
  22. <div v-for="key in keys" :key="key.id" class="table-row even:bg-grey-50 odd:bg-white">
  23. <div class="table-cell p-1 md:p-4">{{ key.name }}</div>
  24. <div class="table-cell p-1 md:p-4">{{ key.created_at | timeAgo }}</div>
  25. <div class="table-cell p-1 md:p-4">
  26. <Toggle v-model="key.enabled" @on="enableKey(key.id)" @off="disableKey(key.id)" />
  27. </div>
  28. <div class="table-cell p-1 md:p-4 text-right">
  29. <a
  30. class="text-red-500 font-bold cursor-pointer focus:outline-none"
  31. @click="showRemoveModal(key)"
  32. >
  33. Delete
  34. </a>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. <Modal :open="deleteKeyModalOpen" @close="closeDeleteKeyModal">
  41. <div class="max-w-lg w-full bg-white rounded-lg shadow-2xl px-6 py-6">
  42. <h2
  43. class="font-semibold text-grey-900 text-2xl leading-tight border-b-2 border-grey-100 pb-4"
  44. >
  45. Remove Hardware Key
  46. </h2>
  47. <p v-if="keys.length === 1" class="my-4 text-grey-700">
  48. Once this key is removed, <b>Two-Factor Authentication</b> will be disabled on your
  49. account.
  50. </p>
  51. <p v-else class="my-4 text-grey-700">
  52. Once this key is removed, <b>Two-Factor Authentication</b> will still be enabled as you
  53. have other hardware keys associated with your account.
  54. </p>
  55. <div class="mt-6">
  56. <button
  57. @click="remove"
  58. class="bg-red-500 hover:bg-red-600 text-white font-bold py-3 px-4 rounded focus:outline-none"
  59. :class="removeKeyLoading ? 'cursor-not-allowed' : ''"
  60. :disabled="removeKeyLoading"
  61. >
  62. Remove
  63. <loader v-if="removeKeyLoading" />
  64. </button>
  65. <button
  66. @click="closeDeleteKeyModal"
  67. class="ml-4 px-4 py-3 text-grey-800 font-semibold bg-white hover:bg-grey-50 border border-grey-100 rounded focus:outline-none"
  68. >
  69. Close
  70. </button>
  71. </div>
  72. </div>
  73. </Modal>
  74. </div>
  75. </template>
  76. <script>
  77. import Modal from './Modal.vue'
  78. import Toggle from './../components/Toggle.vue'
  79. export default {
  80. components: {
  81. Modal,
  82. Toggle,
  83. },
  84. data() {
  85. return {
  86. deleteKeyModalOpen: false,
  87. keys: [],
  88. keyToRemove: null,
  89. loading: false,
  90. removeKeyLoading: false,
  91. }
  92. },
  93. mounted() {
  94. this.getWebauthnKeys()
  95. },
  96. methods: {
  97. getWebauthnKeys() {
  98. axios.get('/webauthn/keys').then(response => {
  99. this.keys = response.data
  100. })
  101. },
  102. showRemoveModal(token) {
  103. this.keyToRemove = token
  104. this.deleteKeyModalOpen = true
  105. },
  106. remove() {
  107. this.removeKeyLoading = true
  108. axios.delete(`/webauthn/keys/${this.keyToRemove.id}`).then(response => {
  109. this.removeKeyLoading = false
  110. this.deleteKeyModalOpen = false
  111. this.keyToRemove = null
  112. if (this.keys.length === 1) {
  113. location.reload()
  114. } else {
  115. this.getWebauthnKeys()
  116. }
  117. })
  118. },
  119. enableKey(id) {
  120. axios
  121. .post(
  122. `/webauthn/enabled-keys`,
  123. JSON.stringify({
  124. id: id,
  125. }),
  126. {
  127. headers: { 'Content-Type': 'application/json' },
  128. }
  129. )
  130. .then(response => {
  131. //
  132. })
  133. .catch(error => {
  134. if (error.response !== undefined) {
  135. this.error(error.response.data)
  136. } else {
  137. this.error()
  138. }
  139. })
  140. },
  141. disableKey(id) {
  142. axios
  143. .delete(`/webauthn/enabled-keys/${id}`)
  144. .then(response => {
  145. //
  146. })
  147. .catch(error => {
  148. if (error.response !== undefined) {
  149. this.error(error.response.data)
  150. } else {
  151. this.error()
  152. }
  153. })
  154. },
  155. closeDeleteKeyModal() {
  156. this.deleteKeyModalOpen = false
  157. },
  158. },
  159. }
  160. </script>