Using sessionstores instead of reinventing the wheel

This commit is contained in:
Jeroen De Meerleer 2023-08-25 12:33:20 +02:00
parent 274832b71f
commit b4c64c2d3d
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 31 additions and 17 deletions

View File

@ -1,4 +1,5 @@
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { localStorageStore} from "@skeletonlabs/skeleton";
export const urls = { export const urls = {
capabilities: "/capabilities", capabilities: "/capabilities",
@ -17,3 +18,5 @@ export const globals = writable({
}, },
} }
}); });
export const sessionvars = localStorageStore('vars', [], {storage: 'session'})

View File

@ -28,10 +28,9 @@
</style> </style>
<script lang="js"> <script lang="js">
import { urls, alerts, globals } from "$lib/stores.js"; import { urls, alerts, globals, sessionvars } from "$lib/stores.js";
import { createEventDispatcher } from 'svelte';
import { browser } from "$app/environment"; import { browser } from "$app/environment";
import {requestor} from "../../../serverrequest.js"; import { requestor, setBaseUrl } from "../../../serverrequest.js";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
var email = ''; var email = '';
@ -43,19 +42,27 @@
globalvars = value; globalvars = value;
}); });
let sessions;
sessionvars.subscribe(value => {
sessions = value;
});
setBaseUrl(globalvars.server.url);
async function handleLogin() { async function handleLogin() {
try { try {
let postdata = {email: email, password: password} let postdata = {email: email, password: password}
let response = await requestor.post(urls.login, JSON.stringify(postdata)); let response = await requestor.post(urls.login, JSON.stringify(postdata));
let responsedata = JSON.parse(response.data); let responsedata = JSON.parse(response.data);
if(response.status != 200) { if(response.status !== 200) {
let message = 'Something went wrong during login' let message = 'Something went wrong during login'
if(typeof responsedata.message !== 'undefined') message = responsedata.message if(typeof responsedata.message !== 'undefined') message = responsedata.message
throw(message) throw(message)
} }
if(!responsedata.success) return alerts.update(data => [...data, {text: 'Login failed: ' + responsedata.message , class: 'error'}]); if(!responsedata.success) return alerts.update(data => [...data, {text: 'Login failed: ' + responsedata.message , class: 'error'}]);
globals.update((globalobj) => ({ ...globalobj, ...{user: responsedata.user }})); globals.update((globalobj) => ({ ...globalobj, ...{user: responsedata.user }}));
sessions.update((sessionobj) => ({ ...sessionobj, ...{authorization: responsedata.authorization }}));
goto('/') goto('/')
} catch (error) { } catch (error) {
return alerts.update(data => [...data, {text: error, class: 'error'}]); return alerts.update(data => [...data, {text: error, class: 'error'}]);
@ -63,8 +70,8 @@
} }
function handleGoToServerSelect() { function handleGoToServerSelect() {
if(sessionStorage.getItem('serverurl') != undefined) { if(typeof sessions.serverurl !== 'undefined') {
if(browser) sessionStorage.removeItem('serverurl'); sessionvars.update(data => { delete data.serverurl; return data;});
globals.update((data) => ({ ...data, ...{server: {url: '', caps: {}}} })); globals.update((data) => ({ ...data, ...{server: {url: '', caps: {}}} }));
} }
} }

View File

@ -16,21 +16,27 @@
</style> </style>
<script lang="js"> <script lang="js">
import { urls, alerts, globals } from "$lib/stores.js"; import {urls, alerts, globals, sessionvars } from "$lib/stores.js";
import {createEventDispatcher, onMount} from 'svelte'; import {onMount} from 'svelte';
import {browser} from "$app/environment";
import { requestor, setBaseUrl } from "../../../serverrequest.js"; import { requestor, setBaseUrl } from "../../../serverrequest.js";
var loginserver; var loginserver;
let sessions;
sessionvars.subscribe(value => {
sessions = value;
});
onMount(() => { onMount(() => {
if(sessionStorage.getItem('serverurl') != undefined) { if(typeof sessions.serverurl !== 'undefined') {
loginserver = sessionStorage.getItem('serverurl'); loginserver = sessions.serverurl;
handleSelectServer(); handleSelectServer();
} }
}) })
async function handleSelectServer() { async function handleSelectServer() {
if(!isValidHttpUrl(loginserver)) { if(!isValidHttpUrl(loginserver)) {
loginserver = 'https://' + loginserver; loginserver = 'https://' + loginserver;
@ -46,9 +52,7 @@
let caps = JSON.parse(response.data); let caps = JSON.parse(response.data);
if(!caps.login) return alerts.update(data => [...data, {text: loginserver + ' is unavailable for login currently', class: 'error'}]); if(!caps.login) return alerts.update(data => [...data, {text: loginserver + ' is unavailable for login currently', class: 'error'}]);
globals.update(data => ({ ...data, ...{server: {url: loginserver, caps: caps}}})); globals.update(data => ({ ...data, ...{server: {url: loginserver, caps: caps}}}));
if(browser) { sessionvars.update((sessionobj) => ({ ...sessionobj, ...{serverurl: loginserver }}));
sessionStorage.setItem('serverurl', loginserver);
}
} catch (error) { } catch (error) {
return alerts.update(data => [...data, {text: loginserver + ' is not a Blackbird Chess server', class: 'error'}]); return alerts.update(data => [...data, {text: loginserver + ' is not a Blackbird Chess server', class: 'error'}]);
} }