~starkingdoms/starkingdoms

ba85890681851529852179ae46a5134a8ba0b3a2 — core 2 years ago 9df232b
store the token on the client
2 files changed, 40 insertions(+), 0 deletions(-)

A starkingdoms-client/src/jwt.ts
M starkingdoms-client/src/routes/menu/main.ts
A starkingdoms-client/src/jwt.ts => starkingdoms-client/src/jwt.ts +31 -0
@@ 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 <https://www.gnu.org/licenses/>.

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

M starkingdoms-client/src/routes/menu/main.ts => starkingdoms-client/src/routes/menu/main.ts +9 -0
@@ 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 =