useAdminPasswordAuthVerify.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, ref } from 'vue'
  3. import { useRoute } from 'vue-router'
  4. import type { AlertVariant } from '#shared/components/CommonAlert/types.ts'
  5. import type {
  6. FormSchemaField,
  7. FormValues,
  8. } from '#shared/components/Form/types.ts'
  9. import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
  10. import { useAdminPasswordAuthVerifyMutation } from '../graphql/mutations/adminPasswordAuthVerify.api.ts'
  11. interface AdminPasswordAuthVerifyOptions {
  12. formChangeFields: Record<string, Partial<FormSchemaField>>
  13. formInitialValues: FormValues
  14. }
  15. export const useAdminPasswordAuthVerify = (
  16. options: AdminPasswordAuthVerifyOptions,
  17. ) => {
  18. const route = useRoute()
  19. const token = route.query.token as string
  20. if (!token) return {}
  21. const verifyToken = new MutationHandler(
  22. useAdminPasswordAuthVerifyMutation({
  23. variables: { token },
  24. }),
  25. {
  26. errorShowNotification: false,
  27. },
  28. )
  29. const verifyTokenResult = ref(false)
  30. const verifyTokenMessage = ref('')
  31. verifyToken
  32. .send()
  33. .then((data) => {
  34. if (data?.adminPasswordAuthVerify?.login) {
  35. options.formChangeFields.login = {
  36. props: {
  37. disabled: true,
  38. },
  39. }
  40. options.formInitialValues.login = data.adminPasswordAuthVerify.login
  41. verifyTokenMessage.value = __(
  42. 'The token is valid. You are now able to login via password once.',
  43. )
  44. verifyTokenResult.value = true
  45. }
  46. })
  47. .catch(() => {
  48. verifyTokenMessage.value = __(
  49. 'The token for the admin password login is invalid.',
  50. )
  51. })
  52. const verifyTokenAlertVariant = computed<AlertVariant>(() => {
  53. return verifyTokenResult.value ? 'success' : 'danger'
  54. })
  55. return {
  56. verifyTokenResult,
  57. verifyTokenMessage,
  58. verifyTokenAlertVariant,
  59. }
  60. }