All files / app/assets/javascripts/authentication/two_factor_auth/components manage_two_factor_form.vue

5.88% Statements 1/17
0% Branches 0/5
0% Functions 0/5
6.25% Lines 1/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168          2x                                                                                                                                                                                                                                                                                                                                    
<script>
import { GlFormInput, GlFormGroup, GlButton, GlForm, GlModal } from '@gitlab/ui';
import csrf from '~/lib/utils/csrf';
import { __ } from '~/locale';
 
export const i18n = {
  currentPassword: __('Current password'),
  confirmTitle: __('Are you sure?'),
  confirmWebAuthn: __('This will invalidate your registered applications and WebAuthn devices.'),
  disableTwoFactor: __('Disable two-factor authentication'),
  disable: __('Disable'),
  cancel: __('Cancel'),
  regenerateRecoveryCodes: __('Regenerate recovery codes'),
  currentPasswordInvalidFeedback: __('Please enter your current password.'),
};
 
export default {
  name: 'ManageTwoFactorForm',
  i18n,
  modalId: 'manage-two-factor-auth-confirm-modal',
  modalActions: {
    primary: {
      text: i18n.disable,
      attributes: {
        variant: 'danger',
      },
    },
    secondary: {
      text: i18n.cancel,
      attributes: {
        variant: 'default',
      },
    },
  },
  components: {
    GlForm,
    GlFormInput,
    GlFormGroup,
    GlButton,
    GlModal,
  },
  inject: [
    'isCurrentPasswordRequired',
    'profileTwoFactorAuthPath',
    'profileTwoFactorAuthMethod',
    'codesProfileTwoFactorAuthPath',
    'codesProfileTwoFactorAuthMethod',
  ],
  data() {
    return {
      method: null,
      action: null,
      currentPassword: '',
      currentPasswordState: null,
      showConfirmModal: false,
    };
  },
  computed: {
    confirmText() {
      return i18n.confirmWebAuthn;
    },
  },
  methods: {
    submitForm() {
      this.$refs.form.$el.submit();
    },
    async handleSubmitButtonClick({ method, action, confirm = false }) {
      this.method = method;
      this.action = action;
 
      Iif (this.isCurrentPasswordRequired && this.currentPassword === '') {
        this.currentPasswordState = false;
 
        return;
      }
 
      this.currentPasswordState = null;
 
      Iif (confirm) {
        this.showConfirmModal = true;
 
        return;
      }
 
      // Wait for form action and method to be updated
      await this.$nextTick();
 
      this.submitForm();
    },
    handleModalPrimary() {
      this.submitForm();
    },
  },
  csrf,
};
</script>
 
<template>
  <gl-form
    ref="form"
    class="gl-sm-display-inline-block"
    method="post"
    :action="action"
    @submit.prevent
  >
    <input type="hidden" name="_method" data-testid="test-2fa-method-field" :value="method" />
    <input :value="$options.csrf.token" type="hidden" name="authenticity_token" />
 
    <gl-form-group
      v-if="isCurrentPasswordRequired"
      :label="$options.i18n.currentPassword"
      label-for="current-password"
      :state="currentPasswordState"
      :invalid-feedback="$options.i18n.currentPasswordInvalidFeedback"
    >
      <gl-form-input
        id="current-password"
        v-model="currentPassword"
        type="password"
        name="current_password"
        :state="currentPasswordState"
      />
    </gl-form-group>
 
    <div class="gl-display-flex gl-flex-wrap">
      <gl-button
        type="submit"
        class="gl-sm-mr-3 gl-w-full gl-sm-w-auto"
        data-testid="test-2fa-disable-button"
        variant="danger"
        @click.prevent="
          handleSubmitButtonClick({
            method: profileTwoFactorAuthMethod,
            action: profileTwoFactorAuthPath,
            confirm: true,
          })
        "
      >
        {{ $options.i18n.disableTwoFactor }}
      </gl-button>
      <gl-button
        type="submit"
        class="gl-mt-3 gl-sm-mt-0 gl-w-full gl-sm-w-auto"
        data-testid="test-2fa-regenerate-codes-button"
        @click.prevent="
          handleSubmitButtonClick({
            method: codesProfileTwoFactorAuthMethod,
            action: codesProfileTwoFactorAuthPath,
          })
        "
      >
        {{ $options.i18n.regenerateRecoveryCodes }}
      </gl-button>
    </div>
    <gl-modal
      v-model="showConfirmModal"
      :modal-id="$options.modalId"
      size="sm"
      :title="$options.i18n.confirmTitle"
      :action-primary="$options.modalActions.primary"
      :action-secondary="$options.modalActions.secondary"
      @primary="handleModalPrimary"
    >
      {{ confirmText }}
    </gl-modal>
  </gl-form>
</template>