save auth and game when refreshing the page
authorGeoffrey Allott <geoffrey@allott.email>
Wed, 24 May 2023 21:23:45 +0000 (22:23 +0100)
committerGeoffrey Allott <geoffrey@allott.email>
Wed, 24 May 2023 21:23:45 +0000 (22:23 +0100)
site/modules/auth.js
site/modules/socket.js

index b5f2323dab801db2690e232e767b926d924dc8eb..a2c44112e08aca4c678ad5e99c02bef09a4d5a91 100644 (file)
@@ -29,4 +29,16 @@ export class Auth {
             signature: this.password
         };
     }
+
+    toString() {
+        return JSON.stringify({
+            "username": this.username,
+            "password": this.password,
+        });
+    }
+
+    static fromString(string) {
+        const auth = JSON.parse(string);
+        return new Auth(auth.username, auth.password);
+    }
 }
index 3a77a09cb65d427129a8fcad95e2e6575c12a653..e6404494e55b99d3cf362c15bd6bf7a79022602e 100644 (file)
@@ -7,6 +7,14 @@ import { Chatroom } from "./chatroom.js";
 import { KnockOutWhist } from "./whist.js";
 import { TexasHoldEm } from "./poker.js";
 
+function create_search_params(key_value_pairs) {
+    const params = new URLSearchParams();
+    for (const [key, value] of key_value_pairs) {
+        params.append(key, value);
+    }
+    return params.toString();
+}
+
 export class Socket {
     constructor(container, login_all_sockets) {
         this.container = container;
@@ -27,6 +35,12 @@ export class Socket {
     onopen() {
         console.log("WebSocket connected");
         this.state = "Connected";
+
+        const auth = window.localStorage.getItem("auth");
+        if (auth !== null) {
+            console.log("Logging in from saved credentials");
+            this.login(Auth.fromString(auth));
+        }
     }
 
     send(object) {
@@ -68,6 +82,18 @@ export class Socket {
                 this.state = "Connected";
                 break;
             case "LoginSuccess":
+                window.localStorage.setItem("auth", this.auth.toString());
+                this.hide_login();
+                this.game = new MainMenu(this.container, message => this.send(message));
+                this.container.append(this.close_button());
+                this.state = "LoggedIn";
+                const params = new URLSearchParams(document.location.hash.substr(1));
+                if (params.has("game")) {
+                    this.send({type: "JoinGame", id: Number(params.get("game"))});
+                } else if (params.has("lobby")) {
+                    this.send({type: "JoinLobby", filter: params.get("lobby")});
+                }
+                break;
             case "LeaveLobbySuccess":
                 this.hide_login();
                 this.game = new MainMenu(this.container, message => this.send(message));
@@ -83,6 +109,7 @@ export class Socket {
                 this.game = new GameList(this.container, message.filter, message.games, message => this.send(message))
                 this.container.append(this.close_button());
                 this.state = "InLobby";
+                document.location.hash = create_search_params([["lobby", message.filter]]);
                 break;
             case "NewGame":
                 this.game.new_game(message.game);
@@ -124,6 +151,20 @@ export class Socket {
                 }
                 break;
         }
+        switch (this.state) {
+            case "Connected":
+            case "LoginAuthResponseSent":
+                break;
+            case "InLobby":
+                document.location.hash = create_search_params([["lobby", this.last_filter]]);
+                break;
+            case "InGame":
+                document.location.hash = create_search_params([["game", this.game.summary.id]]);
+                break;
+            default:
+                document.location.hash = "";
+                break;
+        }
     }
 
     pause_time_for_action(action) {
@@ -235,6 +276,7 @@ export class Socket {
         switch (this.state) {
             case "LoggedIn":
                 this.send({type: "Logout"});
+                window.localStorage.removeItem("auth");
                 break;
             case "InLobby":
                 this.send({type: "LeaveLobby"});