Import.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div class="columns is-centered">
  3. <div class="form-column column is-two-thirds-tablet is-half-desktop is-one-third-widescreen is-one-third-fullhd">
  4. <h1 class="title">
  5. {{ $t('twofaccounts.import.import') }}
  6. </h1>
  7. <div class="is-size-7-mobile">
  8. {{ $t('twofaccounts.import.import_legend')}}
  9. </div>
  10. <div v-if="!migrationUri" class="mt-3 mb-6">
  11. <router-link class="is-link mt-5" :to="{ name: 'start' }">
  12. <font-awesome-icon :icon="['fas', 'plus-circle']" /> {{ $t('twofaccounts.import.use_a_qr_code') }}
  13. </router-link>
  14. </div>
  15. <div v-else>
  16. <div v-if="exportedAccounts.length > 0">
  17. <div v-for="(account, index) in exportedAccounts" :key="account.name" class="group-item has-text-light is-size-5 is-size-6-mobile">
  18. {{ account.account }}
  19. <!-- import button -->
  20. <a class="tag is-dark is-pulled-right" @click="createAccount(index)" :title="$t('twofaccounts.import.import')">
  21. {{ $t('twofaccounts.import.import') }}
  22. </a>
  23. <!-- remove button -->
  24. <a class="tag is-dark is-pulled-right" @click="discardAccount(index)" :title="$t('commons.discard')">
  25. {{ $t('commons.discard') }}
  26. </a>
  27. <span class="is-family-primary is-size-6 is-size-7-mobile has-text-grey">{{ $t('twofaccounts.import.issuer') }}: {{ account.service }}</span>
  28. </div>
  29. <!-- <div class="mt-2 is-size-7 is-pulled-right" v-if="exportedAccounts.length > 0">
  30. {{ $t('groups.deleting_group_does_not_delete_accounts')}}
  31. </div> -->
  32. </div>
  33. <div v-if="isFetching && exportedAccounts.length === 0" class="has-text-centered">
  34. <span class="is-size-4">
  35. <font-awesome-icon :icon="['fas', 'spinner']" spin />
  36. </span>
  37. </div>
  38. </div>
  39. <!-- footer -->
  40. <vue-footer :showButtons="true">
  41. <!-- close button -->
  42. <p class="control">
  43. <router-link :to="{ name: 'accounts', params: { toRefresh: true } }" class="button is-dark is-rounded">{{ $t('commons.close') }}</router-link>
  44. </p>
  45. </vue-footer>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import Form from './../../components/Form'
  51. export default {
  52. data() {
  53. return {
  54. migrationUri: '',
  55. exportedAccounts: [],
  56. isFetching: false,
  57. form: new Form({
  58. service: '',
  59. account: '',
  60. otp_type: '',
  61. icon: '',
  62. secret: '',
  63. secretIsBase32Encoded: 0,
  64. algorithm: '',
  65. digits: null,
  66. counter: null,
  67. period: null,
  68. image: '',
  69. qrcode: null,
  70. }),
  71. }
  72. },
  73. mounted: async function() {
  74. if( this.$route.params.migrationUri ) {
  75. this.migrationUri = this.$route.params.migrationUri
  76. this.isFetching = true
  77. await this.axios.post('/api/v1/twofaccounts/import', { uri: this.migrationUri }).then(response => {
  78. // we should receive an array of twofaccounts
  79. response.data.forEach((data) => {
  80. this.exportedAccounts.push(data)
  81. })
  82. })
  83. .catch(error => {
  84. // if( error.response.status === 422 ) {
  85. // if( error.response.data.errors.uri ) {
  86. // this.showAlternatives = true
  87. // this.showAdvancedForm = true
  88. // }
  89. // }
  90. });
  91. this.isFetching = false
  92. }
  93. else {
  94. // move to error because migration uri is missing
  95. // todo
  96. }
  97. },
  98. created: function() {
  99. },
  100. methods: {
  101. discardAccount(accountId) {
  102. this.exportedAccounts.splice(accountId, 1)
  103. },
  104. async createAccounts() {
  105. for (let i = 0; i < this.exportedAccounts.length; i++) {
  106. await createAccount(i)
  107. }
  108. },
  109. async createAccount(accountId) {
  110. let twofaccount = this.exportedAccounts[accountId]
  111. this.form.account = twofaccount.account
  112. this.form.service = twofaccount.service
  113. this.form.otp_type = twofaccount.otp_type
  114. this.form.secret = twofaccount.secret
  115. this.form.secretIsBase32Encoded = 1
  116. this.form.algorithm = twofaccount.algorithm
  117. this.form.digits = twofaccount.digits
  118. this.form.counter = twofaccount.otp_type === 'hotp' ? twofaccount.counter : null
  119. this.form.period = twofaccount.otp_type === 'totp' ? twofaccount.period : null
  120. await this.form.post('/api/v1/twofaccounts')
  121. if( this.form.errors.any() === false ) {
  122. console.log('account #' + accountId + 'created')
  123. }
  124. },
  125. }
  126. }
  127. </script>