OneTimePassword.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. this.totp = response.data.totp.substr(0, 3) + " " + response.data.totp.substr(3);
  27. this.position = response.data.position;
  28. let dots = this.$el.querySelector('.dots');
  29. // clear active dots
  30. while (dots.querySelector('[data-is-active]')) {
  31. dots.querySelector('[data-is-active]').removeAttribute('data-is-active');
  32. }
  33. // set dot at given position as the active one
  34. let active = dots.querySelector('li:nth-child(' + (this.position + 1 ) + ')');
  35. active.setAttribute('data-is-active', true);
  36. let self = this;
  37. this.timerID = setInterval(function() {
  38. let sibling = active.nextSibling;
  39. if(active.nextSibling === null) {
  40. console.log('no more sibling to activate, we refresh the TOTP')
  41. self.stopLoop()
  42. self.getOTP();
  43. }
  44. else
  45. {
  46. active.removeAttribute('data-is-active');
  47. sibling.setAttribute('data-is-active', true);
  48. active = sibling
  49. }
  50. }, 1000);
  51. })
  52. },
  53. clearOTP: function() {
  54. this.stopLoop()
  55. this.timerID = null
  56. this.totp = '... ...'
  57. this.$el.querySelector('[data-is-active]').removeAttribute('data-is-active');
  58. this.$el.querySelector('.dots li:first-child').setAttribute('data-is-active', true);
  59. },
  60. stopLoop: function() {
  61. clearInterval(this.timerID)
  62. }
  63. },
  64. beforeDestroy () {
  65. this.stopLoop()
  66. }
  67. }
  68. </script>