From 00e7e27183441a32de16d6b4906cc112d8a7fd59 Mon Sep 17 00:00:00 2001 From: Sergei Birukov Date: Fri, 22 Mar 2024 19:59:33 +0300 Subject: [PATCH] Update --- webui/src/App.vue | 257 +++++++----------------- webui/src/components/Auth.vue | 7 +- webui/src/components/ClientControls.vue | 24 ++- webui/src/services/api.js | 15 +- webui/src/store/store.js | 70 ++++++- 5 files changed, 175 insertions(+), 198 deletions(-) diff --git a/webui/src/App.vue b/webui/src/App.vue index 5e88d045..a676dde0 100644 --- a/webui/src/App.vue +++ b/webui/src/App.vue @@ -32,7 +32,7 @@ @@ -143,7 +121,7 @@ @@ -153,7 +131,7 @@ -
+
@@ -46,5 +49,5 @@ import { useStore } from '@/store/store'; import { storeToRefs } from 'pinia'; const store = useStore(); -const { password, authenticating, login } = storeToRefs(store); +const { password, authenticating } = storeToRefs(store); diff --git a/webui/src/components/ClientControls.vue b/webui/src/components/ClientControls.vue index 1d0045de..f1ad079c 100644 --- a/webui/src/components/ClientControls.vue +++ b/webui/src/components/ClientControls.vue @@ -2,18 +2,18 @@
@@ -22,7 +22,7 @@ @@ -41,7 +41,7 @@ @@ -49,15 +49,20 @@ diff --git a/webui/src/services/api.js b/webui/src/services/api.js index 8e36d1de..741bc0de 100644 --- a/webui/src/services/api.js +++ b/webui/src/services/api.js @@ -1,12 +1,14 @@ -const SERVER = 'http://127.0.0.1:51821'; - export default class API { + constructor() { + this.SERVER = 'http://localhost:51821'; //! DEV + } async call({ method, path, body }) { - const res = await fetch(`${SERVER}/api${path}`, { + const res = await fetch(`${this.SERVER}/api${path}`, { method, headers: { 'Content-Type': 'application/json', }, + credentials: 'include', //! DEV body: body ? JSON.stringify(body) : undefined, }); @@ -110,4 +112,11 @@ export default class API { body: { address }, }); } + + async getQrCode({ clientId }) { + return this.call({ + method: 'get', + path: `/wireguard/client/${clientId}/qrcode.svg`, + }); + } } diff --git a/webui/src/store/store.js b/webui/src/store/store.js index 095f2b3c..040a6b3e 100644 --- a/webui/src/store/store.js +++ b/webui/src/store/store.js @@ -1,5 +1,5 @@ import API from '@/services/api'; -import { ref } from 'vue'; +import { ref, reactive } from 'vue'; import { defineStore } from 'pinia'; @@ -9,6 +9,14 @@ export const useStore = defineStore('store', () => { const authenticated = ref(null); const authenticating = ref(false); const password = ref(null); + const requiresPassword = ref(null); + + const clients = ref(null); + const clientsPersist = reactive({}); + const clientToDelete = ref(null); + const clientCreateShowModal = ref(null); + const clientCreateName = ref(''); + const qrcode = ref(null); function login(e) { e.preventDefault(); @@ -19,27 +27,75 @@ export const useStore = defineStore('store', () => { authenticating.value = true; api .createSession({ - password: password, + password: password.value, }) .then(async () => { const session = await api.getSession(); authenticated.value = session.authenticated; requiresPassword.value = session.requiresPassword; - return this.refresh(); + // return this.refresh(); }) .catch((err) => { - alert(err.message || err.toString()); + console.log(err.message || err.toString()); }) .finally(() => { - this.authenticating = false; - this.password = null; + authenticating.value = false; + password.value = null; + }); + } + + function logout(e) { + e.preventDefault(); + + api + .deleteSession() + .then(() => { + authenticated.value = false; + clients.value = null; + }) + .catch((err) => { + alert(err.message || err.toString()); }); } + function createClient() { + if (!clientCreateName.value) return; + + api + .createClient({ name: clientCreateName.value }) + .catch((err) => alert(err.message || err.toString())) + .finally + // () => refresh().catch(console.error) + (); + + clientCreateShowModal.value = null; + } + + function deleteClient(client) { + api + .deleteClient({ clientId: client.id }) + .catch((err) => alert(err.message || err.toString())) + .finally + // () => refresh().catch(console.error) + (); + + clientToDelete.value = null; + } + return { authenticated, authenticating, - login, password, + requiresPassword, + clients, + clientsPersist, + clientToDelete, + clientCreateShowModal, + clientCreateName, + qrcode, + login, + logout, + createClient, + deleteClient, }; });