From 3614018794b5e1dd74ea8c724d5bf2d085e87558 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 15 Apr 2025 16:20:48 -0700 Subject: [PATCH 1/7] web: remove redirect --- web/src/app/_components/webpage.tsx | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/web/src/app/_components/webpage.tsx b/web/src/app/_components/webpage.tsx index 2c64aba..fc386fd 100644 --- a/web/src/app/_components/webpage.tsx +++ b/web/src/app/_components/webpage.tsx @@ -1,25 +1,8 @@ "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}>) { - useEffect(() => { login_check() }, []) - return <>
{children} -- 2.49.1 From 49b9b41085aa52b46c2be50c2b51eab1e45d10aa Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 15 Apr 2025 16:20:52 -0700 Subject: [PATCH 2/7] web: create login button --- web/src/app/_components/header.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/web/src/app/_components/header.tsx b/web/src/app/_components/header.tsx index c2274aa..0618bc9 100644 --- a/web/src/app/_components/header.tsx +++ b/web/src/app/_components/header.tsx @@ -15,6 +15,10 @@ function HeaderButton(header: HeaderButton) { } export default function Header() { + const handleLoginClick = () => { + window.location.href = "https://auth.staging.strafes.net/oauth2/login?redirect=" + window.location.href; + }; + return (
) -- 2.49.1 From 41663624d37cd1e39d644d9a8af967c8e305f522 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 15 Apr 2025 16:43:04 -0700 Subject: [PATCH 3/7] web: conditionally show avatar when logged in --- web/src/app/_components/header.tsx | 31 +++++++++++++++++++++++++++++- web/src/app/ts/User.ts | 5 +++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 web/src/app/ts/User.ts diff --git a/web/src/app/_components/header.tsx b/web/src/app/_components/header.tsx index 0618bc9..30259bf 100644 --- a/web/src/app/_components/header.tsx +++ b/web/src/app/_components/header.tsx @@ -1,6 +1,11 @@ +"use client" + import Link from "next/link" +import Image from "next/image"; import "./styles/header.scss" +import { UserInfo } from "@/app/ts/User"; +import { useState, useEffect } from "react"; interface HeaderButton { name: string, @@ -19,6 +24,21 @@ export default function Header() { window.location.href = "https://auth.staging.strafes.net/oauth2/login?redirect=" + window.location.href; }; + const [valid, setValid] = useState(false) + const [user, setUser] = useState(null) + + useEffect(() => { + async function getLoginInfo() { + const [validateData, userData] = await Promise.all([ + fetch("/api/session/validate").then(validateResponse => validateResponse.json()), + fetch("/api/session/user").then(userResponse => userResponse.json()) + ]); + setValid(validateData) + setUser(userData) + } + getLoginInfo() + }, []) + return (
) diff --git a/web/src/app/ts/User.ts b/web/src/app/ts/User.ts new file mode 100644 index 0000000..98ffb12 --- /dev/null +++ b/web/src/app/ts/User.ts @@ -0,0 +1,5 @@ +export interface UserInfo { + readonly UserID: number, + readonly Username: string, + readonly AvatarURL: string, +} -- 2.49.1 From 6d14047f57817094db3524fa1769e58cc77dc442 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 15 Apr 2025 16:43:21 -0700 Subject: [PATCH 4/7] web: unused imports --- web/src/app/mapfixes/page.tsx | 2 +- web/src/app/submissions/page.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/web/src/app/mapfixes/page.tsx b/web/src/app/mapfixes/page.tsx index 2945e1e..98b31e2 100644 --- a/web/src/app/mapfixes/page.tsx +++ b/web/src/app/mapfixes/page.tsx @@ -1,6 +1,6 @@ 'use client' -import React, { useState, useEffect } from "react"; +import { useState, useEffect } from "react"; import { MapfixList } from "../ts/Mapfix"; import { MapfixCard } from "../_components/mapCard"; import Webpage from "@/app/_components/webpage"; diff --git a/web/src/app/submissions/page.tsx b/web/src/app/submissions/page.tsx index 2ae27dd..8a7c017 100644 --- a/web/src/app/submissions/page.tsx +++ b/web/src/app/submissions/page.tsx @@ -1,6 +1,6 @@ 'use client' -import React, { useState, useEffect } from "react"; +import { useState, useEffect } from "react"; import { SubmissionList } from "../ts/Submission"; import { SubmissionCard } from "../_components/mapCard"; import Webpage from "@/app/_components/webpage"; -- 2.49.1 From c98d1704231bfbef4bb9d098011892a8a4678ac4 Mon Sep 17 00:00:00 2001 From: ic3w0lf Date: Tue, 15 Apr 2025 18:50:40 -0600 Subject: [PATCH 5/7] Remove hardcoded auth URLs --- web/src/app/_components/header.tsx | 4 ++-- web/src/middleware.ts | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/web/src/app/_components/header.tsx b/web/src/app/_components/header.tsx index 30259bf..b611533 100644 --- a/web/src/app/_components/header.tsx +++ b/web/src/app/_components/header.tsx @@ -21,7 +21,7 @@ function HeaderButton(header: HeaderButton) { export default function Header() { const handleLoginClick = () => { - window.location.href = "https://auth.staging.strafes.net/oauth2/login?redirect=" + window.location.href; + window.location.href = "/auth/oauth2/login?redirect=" + window.location.href; }; const [valid, setValid] = useState(false) @@ -50,7 +50,7 @@ export default function Header() { {valid && user ? (
- + {user.Username}/ diff --git a/web/src/middleware.ts b/web/src/middleware.ts index ce20731..0a25a76 100644 --- a/web/src/middleware.ts +++ b/web/src/middleware.ts @@ -1,13 +1,29 @@ import { NextRequest, NextResponse } from "next/server" export const config = { - matcher: ["/api/:path*"], + matcher: ["/api/:path*", "/auth/:path*"], } export function middleware(request: NextRequest) { - if (!process.env.API_HOST) { - throw new Error("env variable \"API_HOST\" is not set") - } - const url = new URL(process.env.API_HOST + request.nextUrl.pathname.replace(/^\/api/, '') + request.nextUrl.search) - return NextResponse.rewrite(url, { request }) + const { pathname, search } = request.nextUrl + + if (pathname.startsWith("/api")) { + if (!process.env.API_HOST) { + throw new Error('env variable "API_HOST" is not set') + } + const apiUrl = new URL(process.env.API_HOST + pathname.replace(/^\/api/, '') + search) + return NextResponse.rewrite(apiUrl, { request }) + } else if (pathname.startsWith("/auth")) { + if (!process.env.AUTH_HOST) { + throw new Error('env variable "AUTH_HOST" is not set') + } + + const authHost = process.env.AUTH_HOST.replace(/\/$/, "") + const path = pathname.replace(/^\/auth/, "") + const redirectUrl = new URL(authHost + path + search) + + return NextResponse.redirect(redirectUrl, 302) + } + + return NextResponse.next() } \ No newline at end of file -- 2.49.1 From 4c1aef9113a4a940f34b6459f662f92cf1ce4529 Mon Sep 17 00:00:00 2001 From: ic3w0lf Date: Tue, 15 Apr 2025 18:57:35 -0600 Subject: [PATCH 6/7] Update README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fec86ec..75dc884 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,11 @@ Prerequisite: golang installed Prerequisite: bun installed -The environment variable `API_HOST` will need to be set for the middleware. +The environment variables `API_HOST` and `AUTH_HOST` will need to be set for the middleware. Example `.env` in web's root: ``` API_HOST="http://localhost:8082/v1/" +AUTH_HOST="http://localhost:8083/" ``` 1. `cd web` -- 2.49.1 From a95e6b7a9a1faefda1390f9e2e4044064b504241 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 15 Apr 2025 18:50:47 -0700 Subject: [PATCH 7/7] docker: add AUTH_HOST env var to docker compose --- compose.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.yaml b/compose.yaml index 4445c1d..e4ddfac 100644 --- a/compose.yaml +++ b/compose.yaml @@ -50,6 +50,7 @@ services: - "3000:3000" environment: - API_HOST=http://submissions:8082/v1 + - AUTH_HOST=http://localhost:8080/ validation: image: -- 2.49.1