From 290218eefedf8fefbb3c329697f6f1a08ec976db Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Sat, 26 Aug 2023 05:48:44 -0400 Subject: [PATCH] Restore logged-in state on page reload --- client/src/state.ts | 3 +++ client/src/util.ts | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 client/src/util.ts diff --git a/client/src/state.ts b/client/src/state.ts index 67c0955..9e08abb 100644 --- a/client/src/state.ts +++ b/client/src/state.ts @@ -2,6 +2,7 @@ import { reactive } from "vue" import { Track } from "./track" import { Tick } from './ticks' import { error } from "./error" +import { getCookie } from "./util"; enum State { Unfetched, @@ -31,6 +32,8 @@ class AppState { constructor() { this.tracks = new Array this.state = State.Unfetched + const name = getCookie("name") + if (name) this.user = { name } } streamUpdatesFromServer() { const source = new EventSource("/api/v1/updates") diff --git a/client/src/util.ts b/client/src/util.ts new file mode 100644 index 0000000..ebbda1c --- /dev/null +++ b/client/src/util.ts @@ -0,0 +1,8 @@ +export function getCookie(key: string): string | null { + const start = document.cookie.indexOf(key + '=') + if(start === -1) return null + let end: number | undefined = document.cookie.indexOf(';', start) + if(end === -1) + end = undefined + return document.cookie.substring(start + key.length + 1, end) +} \ No newline at end of file