TwoFactorConfigurationFlyout.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { ref, computed, useTemplateRef } from 'vue'
  4. import { useTwoFactorPlugins } from '#shared/entities/two-factor/composables/useTwoFactorPlugins.ts'
  5. import { i18n } from '#shared/i18n.ts'
  6. import type { ObjectLike } from '#shared/types/utils.ts'
  7. import CommonFlyout from '#desktop/components/CommonFlyout/CommonFlyout.vue'
  8. import { closeFlyout } from '#desktop/components/CommonFlyout/useFlyout.ts'
  9. import TwoFactorConfigurationPasswordCheck from './TwoFactorConfiguration/TwoFactorConfigurationPasswordCheck.vue'
  10. import TwoFactorConfigurationRecoveryCodes from './TwoFactorConfiguration/TwoFactorConfigurationRecoveryCodes.vue'
  11. import type {
  12. TwoFactorConfigurationActionPayload,
  13. TwoFactorConfigurationComponentInstance,
  14. TwoFactorConfigurationProps,
  15. TwoFactorConfigurationType,
  16. } from './types.ts'
  17. const props = defineProps<TwoFactorConfigurationProps>()
  18. const activeComponentInstance =
  19. useTemplateRef<TwoFactorConfigurationComponentInstance>('active-component')
  20. const flyoutName = 'two-factor-flyout'
  21. const headerTitle = computed(() => {
  22. switch (props.type) {
  23. case 'recovery_codes':
  24. return i18n.t(
  25. 'Generate Recovery Codes: %s',
  26. i18n.t(activeComponentInstance.value?.headerSubtitle),
  27. )
  28. case 'removal_confirmation':
  29. return i18n.t(
  30. 'Remove Two-factor Authentication: %s',
  31. i18n.t(activeComponentInstance.value?.headerSubtitle),
  32. )
  33. default:
  34. return i18n.t(
  35. 'Set Up Two-factor Authentication: %s',
  36. i18n.t(activeComponentInstance.value?.headerSubtitle),
  37. )
  38. }
  39. })
  40. const state = ref<TwoFactorConfigurationType>('password_check')
  41. const componentOptions = ref<ObjectLike>()
  42. const { twoFactorMethodLookup } = useTwoFactorPlugins()
  43. const activeComponent = computed(() => {
  44. switch (state.value) {
  45. case 'recovery_codes':
  46. return TwoFactorConfigurationRecoveryCodes
  47. case 'password_check':
  48. case 'removal_confirmation':
  49. return TwoFactorConfigurationPasswordCheck
  50. default:
  51. return twoFactorMethodLookup[state.value].configurationOptions?.component
  52. }
  53. })
  54. const handleActionPayload = (payload: TwoFactorConfigurationActionPayload) => {
  55. if (!payload?.nextState) {
  56. closeFlyout('two-factor-flyout')
  57. return
  58. }
  59. state.value = payload.nextState
  60. componentOptions.value = payload.options
  61. }
  62. const onFooterButtonAction = () => {
  63. if (activeComponentInstance.value?.footerActionOptions?.form) return
  64. activeComponentInstance.value
  65. ?.executeAction?.()
  66. .then((payload) => handleActionPayload(payload))
  67. .catch(() => {})
  68. }
  69. </script>
  70. <template>
  71. <CommonFlyout
  72. :header-title="headerTitle"
  73. :footer-action-options="activeComponentInstance?.footerActionOptions"
  74. :header-icon="activeComponentInstance?.headerIcon"
  75. :name="flyoutName"
  76. no-close-on-action
  77. @action="onFooterButtonAction"
  78. >
  79. <component
  80. :is="activeComponent"
  81. ref="active-component"
  82. :type="type"
  83. :options="componentOptions"
  84. :form-submit-callback="handleActionPayload"
  85. :success-callback="successCallback"
  86. />
  87. </CommonFlyout>
  88. </template>