LoginRecoveryCode.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import Form from '#shared/components/Form/Form.vue'
  4. import type {
  5. FormSubmitData,
  6. FormSchemaNode,
  7. } from '#shared/components/Form/types.ts'
  8. import { useForm } from '#shared/components/Form/useForm.ts'
  9. import type {
  10. RecoveryCodeFormData,
  11. LoginCredentials,
  12. } from '#shared/entities/two-factor/types.ts'
  13. import UserError from '#shared/errors/UserError.ts'
  14. import { useAuthenticationStore } from '#shared/stores/authentication.ts'
  15. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  16. const props = defineProps<{
  17. credentials: LoginCredentials
  18. }>()
  19. const emit = defineEmits<{
  20. finish: []
  21. error: [error: UserError]
  22. 'clear-error': []
  23. }>()
  24. const schema: FormSchemaNode[] = [
  25. {
  26. type: 'text',
  27. name: 'code',
  28. label: __('Recovery Code'),
  29. required: true,
  30. props: {
  31. help: __('Enter one of your unused recovery codes.'),
  32. },
  33. },
  34. ]
  35. const authentication = useAuthenticationStore()
  36. const { form, isDisabled } = useForm()
  37. const enterRecoveryCode = (formData: FormSubmitData<RecoveryCodeFormData>) => {
  38. // Clear notifications to avoid duplicated error messages.
  39. emit('clear-error')
  40. const { login, password, rememberMe } = props.credentials
  41. return authentication
  42. .login({
  43. login,
  44. password,
  45. rememberMe,
  46. recoveryCode: formData.code,
  47. })
  48. .then(() => {
  49. emit('finish')
  50. })
  51. .catch((error: UserError) => {
  52. if (error instanceof UserError) {
  53. emit('error', error)
  54. }
  55. })
  56. }
  57. </script>
  58. <template>
  59. <Form
  60. ref="form"
  61. :schema="schema"
  62. @submit="enterRecoveryCode($event as FormSubmitData<RecoveryCodeFormData>)"
  63. >
  64. <template #after-fields>
  65. <CommonButton
  66. type="submit"
  67. variant="submit"
  68. size="large"
  69. class="mt-8"
  70. block
  71. :disabled="isDisabled"
  72. >
  73. {{ $t('Sign in') }}
  74. </CommonButton>
  75. </template>
  76. </Form>
  77. </template>