diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index b81e831..65946b4 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -4,8 +4,8 @@ import { backend, set_auth_token, UserInfo } from "../utils"; import { Card } from "../components/Card"; import { AxiosError } from "axios"; -const userIdKey = "UserId"; -const usernameKey = "Username"; +export const userIdKey = "UserId"; +export const usernameKey = "Username"; export function Index() { const [userInfo, setUserInfo] = createSignal(null); diff --git a/src/pages/Lobby.tsx b/src/pages/Lobby.tsx index e30015b..b42dd70 100644 --- a/src/pages/Lobby.tsx +++ b/src/pages/Lobby.tsx @@ -1,11 +1,14 @@ -import { Show, createSignal, onMount } from "solid-js"; +import { Show, createSignal, onCleanup, onMount } from "solid-js"; import LoadingIcon from "../assets/loading.svg"; import ErrorIcon from "../assets/error.svg"; +import { userIdKey } from "./Index"; +import { useNavigate } from "@solidjs/router"; enum LobbyStatus { Connecting, Connected, Disconnected, + Authenticating, Error, } @@ -13,6 +16,8 @@ const connectionRetryInterval = 5000; export function Lobby() { const [status, setStatus] = createSignal(LobbyStatus.Disconnected); + const navigate = useNavigate(); + let ws: WebSocket|null = null; const lobbyConnect = () => { @@ -30,6 +35,20 @@ export function Lobby() { setStatus(LobbyStatus.Connected); // The first message must be authenticating with the server + + const userId = localStorage.getItem(userIdKey); + if (userId === null) { + // Redirect to home page + ws?.close(); + navigate("/"); + return; + } + + // Send an auth message to the server + ws!.send(JSON.stringify({ + action: "auth", + value: userId, + })); }; const onWsMessage = (ev: MessageEvent) => { @@ -65,6 +84,9 @@ export function Lobby() { }; onMount(lobbyConnect); + onCleanup(() => { + ws?.close(); + }); return (
@@ -78,6 +100,12 @@ export function Lobby() { Connecting to the lobby

+ +

+ ... + Logging in to the server +

+