diff --git a/frontend/src/auth.js b/frontend/src/auth.js index 0dfe85f..78710bf 100644 --- a/frontend/src/auth.js +++ b/frontend/src/auth.js @@ -1,5 +1,5 @@ import gateway, { GatewayEventType } from "./gateway"; -import { setAuthToken } from "./storage"; +import { removeItem, setItem } from "./storage"; import { overlayStore } from "./stores"; function useAuthHandlers() { @@ -15,12 +15,12 @@ function useAuthHandlers() { export function authWithToken(token, shouldUpdate=false) { if (shouldUpdate) - setAuthToken(token); + setItem("token", token); gateway.init(token); } export function logOut() { - setAuthToken(null); + removeItem("token"); gateway.close(); gateway.dispatch(GatewayEventType.BadAuth, -1); } diff --git a/frontend/src/gateway.js b/frontend/src/gateway.js index b886ad4..8f94d6a 100644 --- a/frontend/src/gateway.js +++ b/frontend/src/gateway.js @@ -1,5 +1,5 @@ import logging from "./logging"; -import { getAuthToken, getItem } from "./storage"; +import { getItem } from "./storage"; export const GatewayErrors = { BAD_PAYLOAD: 4001, diff --git a/frontend/src/main.js b/frontend/src/main.js index b8fb803..ce56bee 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -1,6 +1,6 @@ import Main from './components/Main.svelte'; import gateway from './gateway'; -import { getAuthToken, initStorageDefaults } from './storage'; +import { getItem, init } from './storage'; import logging from "./logging"; import { authWithToken } from './auth'; import { initResponsiveHandlers } from './responsive'; @@ -10,9 +10,9 @@ window.__waffle = { gateway }; -initStorageDefaults(); +init(); initResponsiveHandlers(); -authWithToken(getAuthToken()); +authWithToken(getItem("token")); // Remove loading screen const loadingElement = document.getElementById("pre--loading-screen"); diff --git a/frontend/src/request.js b/frontend/src/request.js index ca28fbc..3994de6 100644 --- a/frontend/src/request.js +++ b/frontend/src/request.js @@ -1,4 +1,4 @@ -import { getAuthToken } from "./storage"; +import { getItem } from "./storage"; export default async function(method, endpoint, auth=true, body=null) { const options = { @@ -14,7 +14,7 @@ export default async function(method, endpoint, auth=true, body=null) { } if (auth) { - const token = getAuthToken(); + const token = getItem("token"); if (token) { options.headers = { ...options.headers || {}, diff --git a/frontend/src/storage.js b/frontend/src/storage.js index 4f32b95..83b7a36 100644 --- a/frontend/src/storage.js +++ b/frontend/src/storage.js @@ -1,54 +1,46 @@ const defaults = { apiBase: `${window.location.origin}/api/v1`, gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`, - doAnimations: true + doAnimations: true, + token: "" }; +const store = new Map(Object.entries(defaults)); +const persistentProvider = localStorage; -const dummyProvider = { - _store: {}, - getItem(key) { - return this._store[key]; - }, - setItem(key, value) { - this._store[key] = value; +export function setItem(key, value) { + store.set(key, value); + if (persistentProvider) { + persistentProvider.setItem(key, typeof value === "string" ? value : JSON.stringify(value)); } -}; - -function getProvider() { - return window.localStorage || dummyProvider; } export function getItem(key) { - return getProvider().getItem(key); + return store.get(key); } -export function setItem(key, value) { - return getProvider().setItem(key, value); +export function removeItem(key) { + store.delete(key); + if (persistentProvider) { + persistentProvider.removeItem(key); + } } -export function getAuthToken() { - return getItem("token"); -} +export function init() { + if (!persistentProvider) + return; -export function setAuthToken(value) { - return setItem("token", value); + store.forEach((defaultValue, key) => { + const override = persistentProvider.getItem(key); + if (override !== null) { + try { + store.set(key, typeof defaultValue === "string" ? override : JSON.parse(override)); + } catch (o_O) { + console.warn("[Storage]", `init(): An exception was thrown while parsing the value of key "${key}" from _persistentProvider`, o_O); + } + } + }); } export function apiRoute(fragment) { - return `${getItem("apiBase")}/${fragment}`; -} - -export function setItemIfNull(key, value) { - const provider = getProvider(); - if (provider.getItem(key) === undefined) { - provider.setItem(key, value); - return true; - } - return false; -} - -export function initStorageDefaults() { - for (const [k, v] of Object.entries(defaults)) { - setItemIfNull(k, v); - } + return `${getItem("apiBase")}/${fragment}` }