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(); }