From ba85890681851529852179ae46a5134a8ba0b3a2 Mon Sep 17 00:00:00 2001 From: core Date: Sat, 2 Dec 2023 11:50:30 -0500 Subject: [PATCH] store the token on the client --- starkingdoms-client/src/jwt.ts | 31 +++++++++++++++++++++ starkingdoms-client/src/routes/menu/main.ts | 9 ++++++ 2 files changed, 40 insertions(+) create mode 100644 starkingdoms-client/src/jwt.ts diff --git a/starkingdoms-client/src/jwt.ts b/starkingdoms-client/src/jwt.ts new file mode 100644 index 0000000000000000000000000000000000000000..785926845b887317808835a11b1d666d23de84bd --- /dev/null +++ b/starkingdoms-client/src/jwt.ts @@ -0,0 +1,31 @@ +// StarKingdoms.IO, a browser game about drifting through space +// Copyright (C) 2023 ghostly_zsh, TerraMaster85, core +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +export function parseJwt(token: string) { + let base64Url = token.split(".")[1]; + let base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); + let jsonPayload = decodeURIComponent( + window + .atob(base64) + .split("") + .map(function (c) { + return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); + }) + .join(""), + ); + + return JSON.parse(jsonPayload); +} diff --git a/starkingdoms-client/src/routes/menu/main.ts b/starkingdoms-client/src/routes/menu/main.ts index 6971e3da78ed6138b63d697bceb815366025847d..729261e0a639380da65f4b10acf53f922b99f095 100644 --- a/starkingdoms-client/src/routes/menu/main.ts +++ b/starkingdoms-client/src/routes/menu/main.ts @@ -2,6 +2,7 @@ import { ConfigServer, loadConfig } from "../../config.ts"; import createDebug from "debug"; import "../../css/themes/catppuccin-mocha.scss"; import "../../css/style.scss"; +import { parseJwt } from "../../jwt.ts"; // HMR wrapper, to prevent doing reinit twice let initialized: boolean | undefined; @@ -17,6 +18,14 @@ async function init() { initialized = true; + const account_info = document.getElementById("account-info")!; + + if (window.localStorage.getItem("stk-token") != null) { + let token = window.localStorage.getItem("stk-token")!; + let token_parsed: any = parseJwt(token); + account_info.innerText = `Logged in as ${token_parsed.username}! Saves will be stored on the server.`; + } + const is_development = window.localStorage.getItem("stk-mode") === "debug"; document.getElementById("footer-left")!.innerHTML =