TwoFactorConfigurationFlyout.vue 3.1 KB

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