2FAuth/resources/js/components/TwofaccountShow.vue

134 lines
4.8 KiB
Vue
Raw Normal View History

2019-12-21 23:25:19 +01:00
<template>
<div>
2020-01-08 17:03:41 +01:00
<figure class="image is-64x64" style="display: inline-block" v-if="icon">
2020-01-08 23:22:51 +01:00
<img :src="'storage/icons/' + icon">
2019-12-21 23:25:19 +01:00
</figure>
<p class="is-size-4 has-text-grey-light">{{ service }}</p>
<p class="is-size-6 has-text-grey">{{ account }}</p>
2020-01-24 22:37:48 +01:00
<p id="otp" class="is-size-1 has-text-white" :title="$t('commons.copy_to_clipboard')" v-clipboard="() => otp.replace(/ /g, '')" v-clipboard:success="clipboardSuccessHandler">{{ otp }}</p>
<ul class="dots" v-if="type === 'totp'">
<li v-for="n in 30"></li>
</ul>
2020-01-24 22:37:48 +01:00
<ul v-else-if="type === 'hotp'">
2020-01-27 22:18:45 +01:00
<li>counter: {{ counter }}</li>
2020-01-24 22:37:48 +01:00
</ul>
2019-12-21 23:25:19 +01:00
</div>
</template>
<script>
export default {
data() {
return {
id: null,
service: '',
account: '',
icon: '',
2020-01-24 22:37:48 +01:00
type : '',
otp : '',
timerID: null,
position: null,
2020-01-24 22:37:48 +01:00
counter: null,
2019-12-21 23:25:19 +01:00
}
},
methods: {
getAccount: function(id) {
this.id = id
this.axios.get('api/twofaccounts/' + this.id)
.then(async (response) => {
this.service = response.data.service
this.account = response.data.account
this.icon = response.data.icon
2020-01-24 22:37:48 +01:00
this.type = response.data.type
2020-01-24 22:37:48 +01:00
this.type === 'totp' ? await this.getTOTP() : await this.getHOTP()
this.$parent.isActive = true
})
.catch(error => {
this.$router.push({ name: 'genericError', params: { err: error.response } });
});
},
2020-01-24 22:37:48 +01:00
getTOTP: function() {
this.axios.get('api/twofaccounts/' + this.id + '/otp').then(response => {
2020-01-24 22:37:48 +01:00
let spacePosition = Math.ceil(response.data.otp.length / 2);
2020-01-24 22:37:48 +01:00
this.otp = response.data.otp.substr(0, spacePosition) + " " + response.data.otp.substr(spacePosition);
this.position = response.data.position;
let dots = this.$el.querySelector('.dots');
// clear active dots
while (dots.querySelector('[data-is-active]')) {
dots.querySelector('[data-is-active]').removeAttribute('data-is-active');
}
// set dot at given position as the active one
let active = dots.querySelector('li:nth-child(' + (this.position + 1 ) + ')');
active.setAttribute('data-is-active', true);
let self = this;
this.timerID = setInterval(function() {
let sibling = active.nextSibling;
if(active.nextSibling === null) {
2020-01-24 22:37:48 +01:00
console.log('no more sibling to activate, we refresh the OTP')
self.stopLoop()
2020-01-24 22:37:48 +01:00
self.getTOTP();
}
else
{
active.removeAttribute('data-is-active');
sibling.setAttribute('data-is-active', true);
active = sibling
}
}, 1000);
})
},
2020-01-24 22:37:48 +01:00
getHOTP: function() {
this.axios.get('api/twofaccounts/' + this.id + '/otp').then(response => {
2020-01-24 22:37:48 +01:00
let spacePosition = Math.ceil(response.data.otp.length / 2);
this.otp = response.data.otp.substr(0, spacePosition) + " " + response.data.otp.substr(spacePosition);
this.counter = response.data.counter;
})
},
clearOTP: function() {
this.stopLoop()
2020-01-24 22:37:48 +01:00
this.id = this.timerID = this.position = this.counter = null
this.service = this.account = this.icon = this.type = ''
this.otp = '... ...'
this.$el.querySelector('[data-is-active]').removeAttribute('data-is-active');
this.$el.querySelector('.dots li:first-child').setAttribute('data-is-active', true);
},
stopLoop: function() {
clearInterval(this.timerID)
},
clipboardSuccessHandler ({ value, event }) {
console.log('success', value)
},
clipboardErrorHandler ({ value, event }) {
console.log('error', value)
}
},
beforeDestroy () {
this.stopLoop()
}
2019-12-21 23:25:19 +01:00
}
</script>