K356/k356/templates/vue/index.js
2024-09-28 17:37:29 +02:00

118 lines
3.0 KiB
JavaScript

{% include "vue/plugins.js" %}
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(EncryptionPlugin)
Vue.config.delimiters = ["[[", "]]"];
{% for name, path in components.items %}
{% include path %}
Vue.component("{{ name }}", {{ name }})
{% endfor %}
const routes = [
{ path: '/', component: null },
{% for name, path in components.items %}
{
path: {{ name }}.router_path,
name: "{{ name }}",
component: {{ name }},
},
{% endfor %}
]
const encryptionStore = new Vuex.Store({
state: {
aes_key: null,
keyPair: null,
},
mutations: {
update_aes_key (state, key) {
state.aes_key = key
},
update_keyPair (state, keyPair) {
state.keyPair = keyPair
},
}
})
const router = new VueRouter({routes})
const approuter = new Vue({
router,
vuetify: new Vuetify(),
store: encryptionStore,
el: "#main",
data: {
uuid: "{{ user_settings.id }}",
},
computed: {
locked: function() {
return this.$store.state.aes_key == null || this.$store.state.keyPair?.privateKey == null
}
},
mounted: function() {},
methods: {
async load_keys (aes_key) {
const response = await this.$http.get(Urls["users:keys"]())
const iv_private = `${this.uuid}--private`
const iv_public = `${this.uuid}--public`
if (response.data.privateKey != null) {
const keyPair = {
privateKey: await this.unwrapKey(aes_key, response.data.privateKey, iv_private, ["decrypt"]),
publicKey: await this.unwrapKey(aes_key, response.data.publicKey, iv_public, ["encrypt"]),
}
this.$store.commit('update_keyPair', keyPair)
Swal.fire({title: "Successfully loaded K356!", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
} else {
const keyPair = await this.generateKeyPair()
await this.$http.post(
Urls["users:keys"](),
{
privateKey: await this.wrapKey(this.keyPair.privateKey, aes_key, iv_private),
publicKey: await this.wrapKey(this.keyPair.publicKey, aes_key, iv_public),
}
)
this.$store.commit('update_keyPair', keyPair)
Swal.fire({title: "Successfully created K356!", icon: "success", position:"top-end", showConfirmButton: false, toast: true, timer: 1000});
}
},
update_key: function(key) {
this.$store.commit('update_aes_key', key)
this.load_keys(key)
},
lock_me: function() {
this.$store.commit('update_keyPair', null)
this.$store.commit('update_aes_key', null)
}
}
})
router.beforeEach((to, from, next) => {
// Prevent from routing if key is not present.
next(!approuter.locked)
})