summaryrefslogtreecommitdiff
path: root/pse-dashboard/src/store.js
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2024-06-19 00:14:49 +0200
committerOrangerot <purple@orangerot.dev>2024-06-27 12:11:14 +0200
commit5b8851b6c268d0e93c158908fbfae9f8473db5ff (patch)
tree7010eb85d86fa2da06ea4ffbcdb01a685d502ae8 /pse-dashboard/src/store.js
Initial commitHEADmain
Diffstat (limited to 'pse-dashboard/src/store.js')
-rw-r--r--pse-dashboard/src/store.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/pse-dashboard/src/store.js b/pse-dashboard/src/store.js
new file mode 100644
index 0000000..5f838e6
--- /dev/null
+++ b/pse-dashboard/src/store.js
@@ -0,0 +1,54 @@
+import { reactive } from 'vue'
+import { login, logout } from '@/api/pse-squared.js'
+
+const username = sessionStorage.getItem("username") || localStorage.getItem("username");
+const password = sessionStorage.getItem("password") || localStorage.getItem("password");
+
+export const store = reactive({
+ isLoggedIn: username && password,
+ username,
+ password,
+ async login({username, password}, persistant) {
+ try {
+ await login({username, password});
+ this.username = username;
+ this.password = password;
+ this.isLoggedIn = true;
+
+ setStorage(this, persistant);
+
+ return true;
+ } catch(err) {
+ console.error(err);
+ return false;
+ }
+ },
+ async logout() {
+ logout();
+ this.username = "";
+ this.password = "";
+ this.isLoggedIn = false;
+ clearStorage();
+ return true;
+ }
+});
+
+if (username && password) {
+ store.login({username, password});
+}
+
+function setStorage(data, persistant) {
+ if (persistant) {
+ localStorage.setItem("username", data.username);
+ localStorage.setItem("password", data.password);
+ }
+
+ sessionStorage.setItem("username", data.username);
+ sessionStorage.setItem("password", data.password);
+}
+
+function clearStorage() {
+ sessionStorage.clear();
+ localStorage.clear();
+}
+