Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Oct 15, 2024
1 parent 2e19492 commit 0640d62
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
10 changes: 8 additions & 2 deletions api/map/publicData.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down
22 changes: 20 additions & 2 deletions components/auth/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useGoogleLogin } from "@react-oauth/google";
import { inIframe } from "../utils/inIframe";
import { toast } from "react-toastify";

Expand Down Expand Up @@ -28,7 +27,7 @@ export function signIn() {
toast.error("Google client ID not set");
return;
}

window.login();

}
Expand Down Expand Up @@ -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
}
}
11 changes: 9 additions & 2 deletions components/auth/serverAuth.js
Original file line number Diff line number Diff line change
@@ -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;
}
33 changes: 30 additions & 3 deletions components/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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(( ) => {
Expand Down Expand Up @@ -767,15 +789,20 @@ 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) {
secret = session.token.secret;
}


console.log("sending verify with secret", secret)
if(secret !== "not_logged_in") {
window.verified = true;
}
ws.send(JSON.stringify({ type: "verify", secret, tz}))
} else {

Expand Down
13 changes: 11 additions & 2 deletions pages/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -105,6 +111,9 @@ export default function MapPage({ }) {
router.push('/404');
}
}
}).catch(err => {
alert('An error occurred while fetching map data');
// router.push('/404');
});
}, []);

Expand Down
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {

Expand Down

0 comments on commit 0640d62

Please sign in to comment.