makearmy-app/middleware.ts

218 lines
7.6 KiB
TypeScript
Raw Permalink Normal View History

2025-09-26 15:19:19 -04:00
// middleware.ts
2025-09-27 14:30:16 -04:00
import { NextResponse, NextRequest } from "next/server";
/**
* Public pages that should remain reachable without being signed in.
* Everything else is considered protected (including most /api/*).
*/
2025-09-30 22:38:25 -04:00
const PUBLIC_PAGES = new Set<string>([
2025-10-19 23:10:47 -04:00
"/", // splash page is public
2025-09-30 22:38:25 -04:00
"/auth/sign-in",
"/auth/sign-up",
]);
/**
* API paths that are explicitly allowed without auth.
2025-10-19 23:10:47 -04:00
* Keep this list tiny; add broad /api/webhooks to allow ALL webhook endpoints.
*/
const PUBLIC_API_PREFIXES: string[] = [
2025-10-15 21:10:28 -04:00
"/api/auth", // login/refresh/callback endpoints
2025-10-19 23:10:47 -04:00
"/api/files/list", // read-only file endpoints
2025-10-15 21:10:28 -04:00
"/api/files/raw",
"/api/files/download",
2025-10-19 23:10:47 -04:00
"/api/webhooks", // ← allow ALL webhooks (e.g. /api/webhooks/kofi, /api/webhooks/*)
];
/** Directus base (used to remotely validate the token after restarts). */
2025-09-30 22:38:25 -04:00
const DIRECTUS = (process.env.NEXT_PUBLIC_API_BASE_URL || process.env.DIRECTUS_URL || "").replace(/\/$/, "");
type MapResult = { pathname: string; query?: Record<string, string> };
/** Helper: does the path start with any prefix in a list? */
function startsWithAny(pathname: string, prefixes: string[]) {
return prefixes.some((p) => pathname.startsWith(p));
}
/** Helper: are we about to redirect to the same URL? */
function isSameUrl(req: NextRequest, mapped: MapResult) {
const dest = new URL(req.url);
dest.pathname = mapped.pathname;
if (mapped.query) {
2025-09-30 19:35:27 -04:00
for (const [k, v] of Object.entries(mapped.query)) dest.searchParams.set(k, v);
}
return dest.href === req.url;
}
/** Decode JWT exp (seconds since epoch). We don't verify signature here. */
function jwtExp(token: string): number | null {
try {
const [, payload] = token.split(".");
if (!payload) return null;
2025-09-30 19:35:27 -04:00
const json = JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/")));
return typeof json.exp === "number" ? json.exp : null;
} catch {
return null;
}
}
2025-09-30 22:38:25 -04:00
/**
* Build redirect to /auth/sign-in?next=<original>.
* Only set reauth=1 (and clear cookies) when opts.reauth === true.
*/
function kickToSignIn(req: NextRequest, opts?: { reauth?: boolean }) {
const wantReauth = !!opts?.reauth;
2025-09-30 10:36:40 -04:00
const orig = new URL(req.url);
const next = orig.pathname + (orig.search || "");
2025-09-30 22:38:25 -04:00
const url = new URL(req.url);
url.pathname = "/auth/sign-in";
url.search = "";
2025-09-30 22:38:25 -04:00
if (wantReauth) url.searchParams.set("reauth", "1");
2025-09-30 10:36:40 -04:00
url.searchParams.set("next", next);
const res = NextResponse.redirect(url);
2025-09-30 22:38:25 -04:00
// Only clear auth markers in true re-auth scenarios
if (wantReauth) {
res.cookies.set("ma_at", "", { maxAge: 0, path: "/" });
res.cookies.set("ma_v", "", { maxAge: 0, path: "/" }); // throttle marker
2025-10-15 21:10:28 -04:00
// res.cookies.set("ma_rt", "", { maxAge: 0, path: "/" }); // if you use refresh tokens
2025-09-30 22:38:25 -04:00
}
return res;
}
export async function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
const { pathname } = url;
2025-10-19 23:10:47 -04:00
// ── -1) Always allow ALL webhook endpoints (no mapping, no gating, no redirects)
// This lets external providers (Ko-fi, Patreon, etc.) POST without auth.
if (pathname === "/api/webhooks" || pathname.startsWith("/api/webhooks/")) {
return NextResponse.next();
}
2025-10-15 21:10:28 -04:00
// ── 0) Root must never redirect (no mapping, no gating).
if (pathname === "/") return NextResponse.next();
2025-10-15 21:10:28 -04:00
// ── 1) Legacy → Portal mapping (before auth gating)
const mapped = legacyMap(pathname);
if (mapped && !isSameUrl(req, mapped)) {
url.pathname = mapped.pathname;
if (mapped.query) {
2025-09-30 19:35:27 -04:00
for (const [k, v] of Object.entries(mapped.query)) url.searchParams.set(k, v);
}
return NextResponse.redirect(url);
}
2025-10-15 21:10:28 -04:00
// ── 2) Auth gating
const token = req.cookies.get("ma_at")?.value ?? "";
const isAuthRoute = pathname.startsWith("/auth/");
const isProtected = !isPublicPath(pathname);
2025-09-30 10:24:20 -04:00
const forceAuth =
isAuthRoute &&
2025-09-30 19:35:27 -04:00
(url.searchParams.get("reauth") === "1" || url.searchParams.get("force") === "1");
2025-09-30 10:24:20 -04:00
if (!token && isProtected) {
2025-09-30 22:38:25 -04:00
return kickToSignIn(req, { reauth: false });
}
if (token) {
const exp = jwtExp(token);
const expired = !exp || exp * 1000 <= Date.now();
2025-09-30 10:24:20 -04:00
if (isAuthRoute && !expired && !forceAuth) {
url.pathname = "/portal";
url.search = "";
return NextResponse.redirect(url);
}
if (isProtected) {
if (expired) {
2025-09-30 22:38:25 -04:00
return kickToSignIn(req, { reauth: true });
}
if (DIRECTUS) {
const nowMinute = Math.floor(Date.now() / 60000).toString();
const lastValidated = req.cookies.get("ma_v")?.value;
if (lastValidated !== nowMinute) {
try {
const r = await fetch(`${DIRECTUS}/users/me?fields=id`, {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
cache: "no-store",
});
if (!r.ok) {
2025-09-30 22:38:25 -04:00
return kickToSignIn(req, { reauth: true });
}
const res = NextResponse.next();
res.cookies.set("ma_v", nowMinute, {
path: "/",
httpOnly: false,
sameSite: "lax",
2025-10-15 21:10:28 -04:00
maxAge: 90,
});
return res;
} catch {
2025-09-30 22:38:25 -04:00
return kickToSignIn(req, { reauth: true });
}
}
}
}
}
return NextResponse.next();
}
function legacyMap(pathname: string): MapResult | null {
if (pathname === "/" || pathname.startsWith("/portal")) return null;
2025-10-15 21:10:28 -04:00
// detail mappings elided for brevity…
const listRules: Array<[RegExp, MapResult]> = [
[/^\/background-remover\/?$/i, { pathname: "/portal/utilities", query: { t: "background-remover" } }],
2025-10-12 22:31:26 -04:00
[/^\/laser-toolkit\/?$/i, { pathname: "/portal/utilities", query: { t: "laser-toolkit" } }],
[/^\/files\/?$/i, { pathname: "/portal/utilities", query: { t: "files" } }],
[/^\/buying-guide\/?$/i, { pathname: "/portal/buying-guide" }],
2025-10-15 21:10:28 -04:00
[/^\/lasers\/?$/i, { pathname: "/portal/laser-sources" }],
[/^\/projects\/?$/i, { pathname: "/portal/projects" }],
[/^\/my\/rigs\/?$/i, { pathname: "/portal/rigs", query: { t: "my" } }],
];
for (const [re, dest] of listRules) {
if (re.test(pathname)) return dest;
}
return null;
}
function isPublicPath(pathname: string): boolean {
if (PUBLIC_PAGES.has(pathname)) return true;
if (
pathname.startsWith("/_next/") ||
pathname.startsWith("/static/") ||
pathname.startsWith("/images/") ||
pathname === "/favicon.ico" ||
pathname === "/robots.txt" ||
pathname === "/sitemap.xml"
) {
return true;
}
if (pathname.startsWith("/api/")) {
return startsWithAny(pathname, PUBLIC_API_PREFIXES);
}
return false;
}
2025-10-19 23:10:47 -04:00
// Match all except the usual static assets; webhooks are handled above.
export const config = {
2025-09-30 19:35:27 -04:00
matcher: ["/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml|images|static).*)"],
};