OneTimePassword.vue 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <div>
  3. <p id="otp" title="refresh" class="is-size-1 has-text-white">{{ totp }}</p>
  4. <ul class="dots">
  5. <li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
  6. </ul>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. totp : '',
  14. componentKey: 0,
  15. timerID: null,
  16. position: null,
  17. AccountId : null
  18. }
  19. },
  20. methods: {
  21. getOTP: function () {
  22. let token = localStorage.getItem('jwt')
  23. axios.defaults.headers.common['Content-Type'] = 'application/json'
  24. axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
  25. axios.get('api/twofaccounts/' + this.AccountId + '/totp').then(response => {
  26. let spacePosition = Math.ceil(response.data.totp.length / 2);
  27. this.totp = response.data.totp.substr(0, spacePosition) + " " + response.data.totp.substr(spacePosition);
  28. this.position = response.data.position;
  29. let dots = this.$el.querySelector('.dots');
  30. // clear active dots
  31. while (dots.querySelector('[data-is-active]')) {
  32. dots.querySelector('[data-is-active]').removeAttribute('data-is-active');
  33. }
  34. // set dot at given position as the active one
  35. let active = dots.querySelector('li:nth-child(' + (this.position + 1 ) + ')');
  36. active.setAttribute('data-is-active', true);
  37. let self = this;
  38. this.timerID = setInterval(function() {
  39. let sibling = active.nextSibling;
  40. if(active.nextSibling === null) {
  41. console.log('no more sibling to activate, we refresh the TOTP')
  42. self.stopLoop()
  43. self.getOTP();
  44. }
  45. else
  46. {
  47. active.removeAttribute('data-is-active');
  48. sibling.setAttribute('data-is-active', true);
  49. active = sibling
  50. }
  51. }, 1000);
  52. })
  53. },
  54. clearOTP: function() {
  55. this.stopLoop()
  56. this.timerID = null
  57. this.totp = '... ...'
  58. this.$el.querySelector('[data-is-active]').removeAttribute('data-is-active');
  59. this.$el.querySelector('.dots li:first-child').setAttribute('data-is-active', true);
  60. },
  61. stopLoop: function() {
  62. clearInterval(this.timerID)
  63. }
  64. },
  65. beforeDestroy () {
  66. this.stopLoop()
  67. }
  68. }
  69. </script>