From 0640d627b6036a05ecba4f2c9ab42f0b8c8c5762 Mon Sep 17 00:00:00 2001 From: Gautam Anand Date: Tue, 15 Oct 2024 07:08:15 -0500 Subject: [PATCH] Fixes --- api/map/publicData.js | 10 ++++++++-- components/auth/auth.js | 22 ++++++++++++++++++++-- components/auth/serverAuth.js | 11 +++++++++-- components/home.js | 33 ++++++++++++++++++++++++++++++--- pages/map.js | 13 +++++++++++-- server.js | 4 +++- 6 files changed, 81 insertions(+), 12 deletions(-) diff --git a/api/map/publicData.js b/api/map/publicData.js index 5b173b53..ade78ce6 100644 --- a/api/map/publicData.js +++ b/api/map/publicData.js @@ -1,4 +1,4 @@ -import { getServerSession } from "../../components/auth/serverAuth.js"; +import { getServerSecret } from "../../components/auth/serverAuth.js"; import officialCountryMaps from "../../public/officialCountryMaps.json" with { type: "json" }; import Map from "../../models/Map.js"; import User from "../../models/User.js"; @@ -7,7 +7,13 @@ import msToTime from "../../components/msToTime.js"; export default async function handler(req, res) { const slug = req.query.slug; console.log("Getting map data for", slug); - const session = await getServerSession(req); + const secret = await getServerSecret(req); + const session = {}; + if(secret) { + await User.findOne({ secret }).select("secret staff").then((user) => { + session.token = { secret, staff: user.staff }; + }); + } // Check if map is an official country map const cntryMap = Object.values(officialCountryMaps).find(map => map.slug === slug); diff --git a/components/auth/auth.js b/components/auth/auth.js index a230e36b..b0bf7108 100644 --- a/components/auth/auth.js +++ b/components/auth/auth.js @@ -1,4 +1,3 @@ -import { useGoogleLogin } from "@react-oauth/google"; import { inIframe } from "../utils/inIframe"; import { toast } from "react-toastify"; @@ -28,7 +27,7 @@ export function signIn() { toast.error("Google client ID not set"); return; } - + window.login(); } @@ -88,4 +87,23 @@ export function useSession() { return { data: session } +} + +export function getHeaders() { + let secret = null; + if(session && session?.token?.secret) { + secret = session.secret; + } else { + try { + secret = window.localStorage.getItem("wg_secret"); + } catch (e) { + console.error(e); + } + } + if(!secret) { + return {}; + } + return { + Authorization: "Bearer "+secret + } } \ No newline at end of file diff --git a/components/auth/serverAuth.js b/components/auth/serverAuth.js index f7f3cfc3..2c87b7eb 100644 --- a/components/auth/serverAuth.js +++ b/components/auth/serverAuth.js @@ -1,5 +1,12 @@ -export function getServerSession(req, res, authOptions) { - console.log("Getting server session"); +export function getServerSecret(req) { + + // its in headers Bearer token + const authHeader = req.headers.authorization; + if (authHeader) { + const token = authHeader.split(' ')[1]; + return token; + } + return null; } \ No newline at end of file diff --git a/components/home.js b/components/home.js index e3f036c4..44084220 100644 --- a/components/home.js +++ b/components/home.js @@ -155,13 +155,25 @@ export default function Home({ }) { const [legacyMapLoader, setLegacyMapLoader] = useState(false); useEffect(() => { - console.log("setting session", mainSession) if (!inCrazyGames) { setSession(mainSession) } }, [JSON.stringify(mainSession), inCrazyGames]) + useEffect(() => { + window.onbeforeunload = function(e) { + if(screen === "home") { + + } else { + e.preventDefault(); + return e.returnValue = 'Are you sure you want to leave?'; + } + } + }, [screen]) + + + const [config, setConfig] = useState(null); useEffect(() => { @@ -617,6 +629,16 @@ setShowCountryButtons(false) const [multiplayerChatOpen, setMultiplayerChatOpen] = useState(false); const [multiplayerChatEnabled, setMultiplayerChatEnabled] = useState(false); + useEffect(() => { + if(!session?.token?.secret) return; + + // verify the ws + if(ws && !window.verified) { + console.log("sending verify", ws) + ws.send(JSON.stringify({ type: "verify", secret: session.token.secret, username: session.token.username })) + } + }, [session?.token?.secret, ws]) + const { t: text } = useTranslation("common"); useEffect(( ) => { @@ -767,7 +789,10 @@ setShowCountryButtons(false) const tz = moment.tz.guess(); let secret = "not_logged_in"; try { - secret = window.localStorage.getItem("wg_secret"); + const s = window.localStorage.getItem("wg_secret"); + if(s) { + secret = s; + } } catch(e) { } if(session?.token?.secret) { @@ -775,7 +800,9 @@ setShowCountryButtons(false) } - console.log("sending verify with secret", secret) + if(secret !== "not_logged_in") { + window.verified = true; + } ws.send(JSON.stringify({ type: "verify", secret, tz})) } else { diff --git a/pages/map.js b/pages/map.js index 2250add9..a171ebea 100644 --- a/pages/map.js +++ b/pages/map.js @@ -5,6 +5,8 @@ import styles from '@/styles/MapPage.module.css'; // Import CSS module for styli import Navbar from '@/components/ui/navbar'; import { useTranslation } from '@/components/useTranslations' import config from '@/clientConfig'; +import { getHeaders } from '@/components/auth/auth'; +import { toast } from 'react-toastify'; // export async function getServerSideProps(context) { @@ -93,8 +95,12 @@ export default function MapPage({ }) { if (!slug) return; - console.log('fetching map data for', slug); - fetch(apiUrl+`/api/map/publicData?slug=${slug}`).then(async res => { + console.log('fetching map data for', slug, getHeaders()); + fetch(apiUrl+`/api/map/publicData?slug=${slug}`,{ + headers: { + authorization: getHeaders()?.authorization + } + }).then(async res => { if (res.ok) { const data = await res.json(); console.log('fetched map data:', data); @@ -105,6 +111,9 @@ export default function MapPage({ }) { router.push('/404'); } } + }).catch(err => { + alert('An error occurred while fetching map data'); + // router.push('/404'); }); }, []); diff --git a/server.js b/server.js index 0fed746e..8744a422 100644 --- a/server.js +++ b/server.js @@ -1014,9 +1014,10 @@ Key is saved at: /etc/letsencrypt/live/www.worldguessr.com/privkey.pem if (json.type === "pong") { player.lastPong = Date.now(); } - if (json.type === 'verify' && !player.verified) { + if (json.type === 'verify') { // account verification if((!json.secret) || json.secret === 'not_logged_in') { + if(!player.verified) { // guest mode player.username = 'Guest #' + make6DigitCode().toString().substring(0, 4); @@ -1031,6 +1032,7 @@ Key is saved at: /etc/letsencrypt/live/www.worldguessr.com/privkey.pem c: players.size }) console.log('Guest joined', id, player.username); + } } else {