AdminPasswordAuth.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed, ref } from 'vue'
  4. import { useRouter } from 'vue-router'
  5. import Form from '#shared/components/Form/Form.vue'
  6. import type { FormSubmitData } from '#shared/components/Form/types.ts'
  7. import { useForm } from '#shared/components/Form/useForm.ts'
  8. import { useThirdPartyAuthentication } from '#shared/composables/authentication/useThirdPartyAuthentication.ts'
  9. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  10. import { useApplicationStore } from '#shared/stores/application.ts'
  11. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  12. import LayoutPublicPage from '#desktop/components/layout/LayoutPublicPage/LayoutPublicPage.vue'
  13. import { useAdminPasswordAuthSendMutation } from '../graphql/mutations/adminPasswordAuthSend.api.ts'
  14. import type { AdminPasswordAuthRequestData } from '../types/admin-password-auth'
  15. defineOptions({
  16. beforeRouteEnter(to) {
  17. const application = useApplicationStore()
  18. const { hasEnabledProviders } = useThirdPartyAuthentication()
  19. if (application.config.user_show_password_login) {
  20. return to.redirectedFrom ? false : '/'
  21. }
  22. if (!hasEnabledProviders.value) {
  23. return to.redirectedFrom ? false : '/'
  24. }
  25. return true
  26. },
  27. })
  28. const router = useRouter()
  29. const adminPasswordAuthRequestSchema = [
  30. {
  31. name: 'login',
  32. type: 'text',
  33. label: __('Username / Email'),
  34. required: true,
  35. },
  36. ]
  37. const { form, isDisabled } = useForm()
  38. const requestSent = ref(false)
  39. const adminPasswordAuthError = ref('')
  40. const pageTitle = computed(() => {
  41. if (requestSent.value) {
  42. return __(
  43. 'Admin password login instructions were sent to your email address.',
  44. )
  45. }
  46. return __('Request password login for admin?')
  47. })
  48. const send = (data: AdminPasswordAuthRequestData) => {
  49. const sendAdminPasswordAuth = new MutationHandler(
  50. useAdminPasswordAuthSendMutation({
  51. variables: { login: data.login },
  52. }),
  53. {
  54. errorShowNotification: false,
  55. },
  56. )
  57. sendAdminPasswordAuth
  58. .send()
  59. .then(() => {
  60. requestSent.value = true
  61. })
  62. .catch(() => {
  63. adminPasswordAuthError.value = __(
  64. 'The admin password auth email could not be sent.',
  65. )
  66. })
  67. }
  68. const goToLogin = () => {
  69. router.replace('/login')
  70. }
  71. const retry = () => {
  72. requestSent.value = false
  73. }
  74. </script>
  75. <template>
  76. <LayoutPublicPage box-size="medium" :show-logo="false" :title="pageTitle">
  77. <CommonAlert v-if="adminPasswordAuthError" variant="danger">{{
  78. $t(adminPasswordAuthError)
  79. }}</CommonAlert>
  80. <Form
  81. v-if="!requestSent"
  82. id="admin-password-auth"
  83. ref="form"
  84. form-class="mb-2.5"
  85. :schema="adminPasswordAuthRequestSchema"
  86. @submit="send($event as FormSubmitData<AdminPasswordAuthRequestData>)"
  87. />
  88. <CommonLabel v-if="requestSent">
  89. {{
  90. $t(
  91. "If you don't receive instructions within a minute or two, check your email's spam and junk filters, or try resending your request.",
  92. )
  93. }}
  94. </CommonLabel>
  95. <template #boxActions>
  96. <CommonButton
  97. variant="secondary"
  98. size="medium"
  99. :disabled="isDisabled"
  100. @click="goToLogin()"
  101. >
  102. {{ $t('Cancel & Go Back') }}
  103. </CommonButton>
  104. <CommonButton
  105. v-if="!requestSent"
  106. variant="submit"
  107. type="submit"
  108. size="medium"
  109. form="admin-password-auth"
  110. :disabled="isDisabled"
  111. >
  112. {{ $t('Submit') }}
  113. </CommonButton>
  114. <CommonButton v-else variant="submit" size="medium" @click="retry()">
  115. {{ $t('Retry') }}
  116. </CommonButton>
  117. </template>
  118. </LayoutPublicPage>
  119. </template>