diff --git a/bun.lockb b/bun.lockb index 75d36cd..ae6d290 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/Shell.tsx b/src/Shell.tsx index 30ee15d..be57585 100644 --- a/src/Shell.tsx +++ b/src/Shell.tsx @@ -17,8 +17,7 @@ import { useMediaQuery } from "@uidotdev/usehooks"; import Header from "./components/Header"; import { Tag } from "./components/Tag"; import Feed from "./components/Feed/Feed"; -import { useAtom } from "jotai"; -import { loginTokenAtom } from "./atoms"; +import { useWSQuery } from "./hooks"; const drawerWidth = 256; const drawerWidthWithPadding = drawerWidth; @@ -26,7 +25,7 @@ const drawerWidthWithPadding = drawerWidth; const Shell: React.FC = ({ children }) => { const [isOpen, setIsOpen] = useState(false); const drawerRef = useRef(null); - const [loginToken] = useAtom(loginTokenAtom); + const { data: username } = useWSQuery("user.getSelf", null); const x = isOpen ? 0 : -drawerWidthWithPadding; const width = isOpen ? drawerWidthWithPadding : 0; @@ -82,7 +81,7 @@ const Shell: React.FC = ({ children }) => { Play - {loginToken && ( + {username && ( History @@ -92,13 +91,13 @@ const Shell: React.FC = ({ children }) => { Store - {loginToken && ( + {username && ( Collection NEW )} - {loginToken && ( + {username && ( Settings diff --git a/src/components/Auth/RegisterButton.tsx b/src/components/Auth/RegisterButton.tsx index 5eee063..4b1bcc0 100644 --- a/src/components/Auth/RegisterButton.tsx +++ b/src/components/Auth/RegisterButton.tsx @@ -15,7 +15,7 @@ import { useQueryClient } from "@tanstack/react-query"; import PasswordInput from "./PasswordInput"; import { wsClient } from "../../wsClient"; -const RegisterButton = () => { +const RegisterButton = ({ label }: { label?: string }) => { const [isOpen, setIsOpen] = useState(false); const [isLoginMode, setIsLoginMode] = useState(false); const [username, setUsername] = useState(""); @@ -36,7 +36,7 @@ const RegisterButton = () => { diff --git a/src/main.tsx b/src/main.tsx index 26aafbc..5b3da30 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -15,17 +15,49 @@ import Collection from "./views/collection/Collection.tsx"; import { AnimatePresence } from "motion/react"; import Store from "./views/store/Store.tsx"; import Profile from "./views/profile/Profile.tsx"; +import { useWSQuery } from "./hooks.ts"; +import RegisterButton from "./components/Auth/RegisterButton.tsx"; + +const ProtectedRoute: React.FC<{ + component: React.ComponentType; + path: string; +}> = ({ component: Component, path }) => { + const { data: username, isLoading } = useWSQuery("user.getSelf", null); + + return ( + + {(params) => { + if (isLoading) return null; + if (!username) { + return ( +
+

+ This page is only available to logged-in users. +

+ +
+ ); + } + return ; + }} +
+ ); +}; const setup = async () => { const token = localStorage.getItem("loginToken"); if (token) { try { - await wsClient.dispatch("user.loginWithToken", { + const res = await wsClient.dispatch("user.loginWithToken", { token: JSON.parse(token), }); + if (!res.success) { + localStorage.removeItem("loginToken"); + } } catch (e) { console.error(e); + localStorage.removeItem("loginToken"); } } }; @@ -41,9 +73,9 @@ setup().then(() => { {(params) => } - - - + + + {(params) => } diff --git a/src/views/endless/Endless.tsx b/src/views/endless/Endless.tsx index 0e999ff..a500d7f 100644 --- a/src/views/endless/Endless.tsx +++ b/src/views/endless/Endless.tsx @@ -1,6 +1,6 @@ import { useWSMutation, useWSQuery } from "../../hooks"; import { useAtom } from "jotai"; -import { gameIdAtom, loginTokenAtom } from "../../atoms"; +import { gameIdAtom } from "../../atoms"; import { Button } from "../../components/Button"; import LeaderboardButton from "../../components/LeaderboardButton"; import { ShareButton } from "../../components/ShareButton"; @@ -15,7 +15,6 @@ interface EndlessProps { const Endless: React.FC = (props) => { const [gameId, setGameId] = useAtom(gameIdAtom); - const [loginToken] = useAtom(loginTokenAtom); const { data: game } = useWSQuery("game.getGameState", gameId!, !!gameId); const { data: settings } = useWSQuery("user.getSettings", null); const { data: currentUsername } = useWSQuery("user.getSelf", null); @@ -93,7 +92,7 @@ const Endless: React.FC = (props) => {

Minesweeper Endless

- {loginToken ? ( + {currentUsername ? ( ) : ( - + )}

How to play

diff --git a/src/views/store/Store.tsx b/src/views/store/Store.tsx index 3cb6651..b6c1b17 100644 --- a/src/views/store/Store.tsx +++ b/src/views/store/Store.tsx @@ -18,11 +18,13 @@ import { useEffect } from "react"; import { initParticlesEngine, Particles as ParticlesComponent } from "@tsparticles/react"; import { motion } from "motion/react"; import BounceImg from "../../components/BounceImg"; +import RegisterButton from "../../components/Auth/RegisterButton"; const Store = () => { const openLootbox = useWSMutation("user.openLootbox"); const [lootboxResult, setLootboxResult] = useAtom(lootboxResultAtom); const currentLootbox = lootboxes.find((l) => l.id === lootboxResult?.lootbox); + const { data: username } = useWSQuery("user.getSelf", null); const { refetch } = useWSQuery("user.getOwnGems", null); // this should be run only once per application lifetime @@ -184,18 +186,24 @@ const Store = () => {

- + {username ? ( + + ) : ( +
+ +
+ )} ))}