TwoFactorConfigurationFlyout.vue 3.1 KB

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