From b02b3d205e4b02c8616cdbf3285adb504570c67f Mon Sep 17 00:00:00 2001
From: rhpidfyre <brandon@rhpidfyre.io>
Date: Thu, 27 Mar 2025 21:47:03 +0000
Subject: [PATCH] Switch to using `/api/session/validate` for determining if
 the user is not logged in (#34)

My apologies for being stupid not knowing the NextJS framework fully, as I have little experience with it and its non intuitive SSR and CSR workflow

Code successfully built locally running `bun run build`

Reviewed-on: https://git.itzana.me/StrafesNET/maps-service/pulls/34
Co-authored-by: rhpidfyre <brandon@rhpidfyre.io>
Co-committed-by: rhpidfyre <brandon@rhpidfyre.io>
---
 web/src/app/_components/webpage.tsx | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/web/src/app/_components/webpage.tsx b/web/src/app/_components/webpage.tsx
index 6119987..2c64aba 100644
--- a/web/src/app/_components/webpage.tsx
+++ b/web/src/app/_components/webpage.tsx
@@ -1,8 +1,27 @@
+"use client"
+
+import { redirect } from "next/navigation";
+import { useEffect } from "react";
+
 import Header from "./header";
 
+async function login_check() {
+	const response = await fetch("/api/session/validate")
+	if (response.ok) {
+		const logged_in = await response.json()
+		if (!logged_in) {
+			redirect("https://auth.staging.strafes.net/oauth2/login?redirect=" + window.location.href)
+		}
+	} else {
+		console.error("No response from /api/session/validate")
+	}
+}
+
 export default function Webpage({children}: Readonly<{children?: React.ReactNode}>) {
-    return (<>
-        <Header/>
-        {children}
-    </>)
-}
\ No newline at end of file
+	useEffect(() => { login_check() }, [])
+
+	return <>
+		<Header/>
+		{children}
+	</>
+}