2FAuth/resources/js/views/Create.vue

212 lines
8.5 KiB
Vue
Raw Normal View History

<template>
<div class="section">
<div class="columns is-mobile is-centered">
<div class="column is-two-thirds-tablet is-half-desktop is-one-third-widescreen is-one-quarter-fullhd">
<h1 class="title">New account</h1>
<form @submit.prevent="createAccount">
<div class="field">
<div class="file is-dark is-boxed">
2020-01-08 14:56:31 +01:00
<label class="file-label" title="Use a QR code to fill the form magically">
<input class="file-input" type="file" accept="image/*" v-on:change="uploadQrcode" ref="qrcodeInput">
<span class="file-cta">
<span class="file-icon">
<font-awesome-icon :icon="['fas', 'qrcode']" size="lg" />
</span>
2020-01-08 14:56:31 +01:00
<span class="file-label">Use a qrcode</span>
</span>
</label>
2020-01-02 00:09:19 +01:00
</div>
</div>
<div class="field">
<label class="label">Service</label>
<div class="control">
2020-01-08 14:56:31 +01:00
<input class="input" type="text" placeholder="example.com" v-model="twofaccount.service" required autofocus />
</div>
</div>
<div class="field">
<label class="label">Account</label>
<div class="control">
2020-01-08 14:56:31 +01:00
<input class="input" type="text" placeholder="John DOE" v-model="twofaccount.account" />
</div>
</div>
2020-01-08 14:56:31 +01:00
<div class="field" style="margin-bottom: 0.5rem;">
<label class="label">TOTP Uri</label>
</div>
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" type="text" placeholder="otpauth://totp/..." v-model="twofaccount.uri" :disabled="uriIsLocked" />
</div>
<div class="control" v-if="uriIsLocked">
<a class="button is-dark field-lock" @click="uriIsLocked = false" title="Unlock it (at your own risk)">
<span class="icon">
<font-awesome-icon :icon="['fas', 'lock']" />
</span>
</a>
</div>
<div class="control" v-else>
<a class="button is-dark field-unlock" @click="uriIsLocked = true" title="Lock it">
<span class="icon has-text-danger">
<font-awesome-icon :icon="['fas', 'lock-open']" />
</span>
</a>
</div>
</div>
<div class="field">
<label class="label">Icon</label>
<div class="file is-dark">
<label class="file-label">
<input class="file-input" type="file" accept="image/*" v-on:change="uploadIcon" ref="iconInput">
<span class="file-cta">
<span class="file-icon">
<font-awesome-icon :icon="['fas', 'image']" />
</span>
<span class="file-label">Choose an image</span>
</span>
</label>
2020-01-08 23:22:51 +01:00
<span class="tag is-black is-large" v-if="tempIcon">
<img class="icon-preview" :src="'storage/icons/' + tempIcon" >
<button class="delete is-small" @click.prevent="deleteIcon"></button>
2020-01-08 14:56:31 +01:00
</span>
</div>
</div>
<div class="field is-grouped">
<div class="control">
2020-01-08 14:56:31 +01:00
<button type="submit" class="button is-link">Create</button>
</div>
<div class="control">
2020-01-08 23:22:51 +01:00
<button class="button is-text" @click="cancelCreation">Cancel</button>
</div>
</div>
</form>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
2020-01-05 23:21:28 +01:00
twofaccount: {
'service' : '',
'account' : '',
2020-01-05 23:21:28 +01:00
'uri' : '',
'icon' : ''
2020-01-08 14:56:31 +01:00
},
2020-01-08 23:22:51 +01:00
uriIsLocked: true,
tempIcon: ''
}
},
methods: {
createAccount: function() {
2020-01-08 23:22:51 +01:00
// set current temp icon as account icon
this.twofaccount.icon = this.tempIcon
// store the account
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
axios.post('/api/twofaccounts', this.twofaccount).then(response => {
2020-01-07 23:10:04 +01:00
this.$router.push({name: 'accounts', params: { InitialEditMode: false }});
})
2020-01-03 17:25:56 +01:00
},
2020-01-08 23:22:51 +01:00
cancelCreation: function() {
// clean possible uploaded temp icon
if( this.tempIcon ) {
this.deleteIcon()
}
this.$router.push({name: 'accounts', params: { InitialEditMode: false }});
},
2020-01-03 17:25:56 +01:00
uploadQrcode(event) {
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
2020-01-08 23:22:51 +01:00
let files = this.$refs.qrcodeInput.files
2020-01-03 17:25:56 +01:00
2020-01-08 23:22:51 +01:00
if (!files.length) {
console.log('no files');
return false;
}
else {
2020-01-03 17:25:56 +01:00
console.log(files.length + ' file(s) found');
2020-01-08 23:22:51 +01:00
}
2020-01-03 17:25:56 +01:00
let imgdata = new FormData();
imgdata.append('qrcode', files[0]);
let config = {
header : {
'Content-Type' : 'multipart/form-data',
}
}
axios.post('/api/qrcode/decode', imgdata, config).then(response => {
console.log('image upload response > ', response);
this.twofaccount = response.data;
}
)
2020-01-05 23:21:28 +01:00
},
uploadIcon(event) {
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
2020-01-08 23:22:51 +01:00
let files = this.$refs.iconInput.files
2020-01-05 23:21:28 +01:00
2020-01-08 23:22:51 +01:00
if (!files.length) {
return false;
}
2020-01-08 15:24:34 +01:00
2020-01-08 23:22:51 +01:00
// clean possible already uploaded temp icon
if( this.tempIcon ) {
2020-01-08 15:24:34 +01:00
this.deleteIcon()
2020-01-08 23:22:51 +01:00
}
2020-01-05 23:21:28 +01:00
let imgdata = new FormData();
imgdata.append('icon', files[0]);
let config = {
header : {
'Content-Type' : 'multipart/form-data',
}
}
axios.post('/api/icon/upload', imgdata, config).then(response => {
console.log('icon path > ', response);
2020-01-08 23:22:51 +01:00
this.tempIcon = response.data;
2020-01-05 23:21:28 +01:00
}
)
2020-01-08 15:24:34 +01:00
},
deleteIcon(event) {
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
2020-01-08 23:22:51 +01:00
axios.delete('/api/icon/delete/' + this.tempIcon).then(response => {
this.tempIcon = ''
2020-01-08 15:24:34 +01:00
}
)
}
2020-01-08 16:14:25 +01:00
},
}
</script>