LoginRecoveryCode.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 { useForm } from '#shared/components/Form/useForm.ts'
  5. import type {
  6. FormSubmitData,
  7. FormSchemaNode,
  8. } from '#shared/components/Form/types.ts'
  9. import UserError from '#shared/errors/UserError.ts'
  10. import { useAuthenticationStore } from '#shared/stores/authentication.ts'
  11. import type {
  12. RecoveryCodeFormData,
  13. LoginCredentials,
  14. } from '#shared/entities/two-factor/types.ts'
  15. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  16. const props = defineProps<{
  17. credentials: LoginCredentials
  18. }>()
  19. const emit = defineEmits<{
  20. (e: 'finish'): void
  21. (e: 'error', error: UserError): void
  22. (e: 'clear-error'): void
  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>