mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 10:27:56 +01:00
Next.js/Vercel rejects having both middleware.ts and proxy.ts. The existing proxy.ts already protected all app routes and handles the onboarding redirect. Extended it with /sign-out and /thoughts as public paths. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { auth } from "@/auth";
|
|
import { NextResponse } from "next/server";
|
|
|
|
const PUBLIC_PATHS = new Set(["/", "/sign-in", "/sign-out", "/setup"]);
|
|
|
|
export default auth((req) => {
|
|
const { pathname, search } = req.nextUrl;
|
|
|
|
// Static/API paths — let them through (API routes handle their own auth)
|
|
if (
|
|
pathname.startsWith("/_next") ||
|
|
pathname.startsWith("/api/") ||
|
|
pathname === "/favicon.ico"
|
|
) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// Public pages, invite registration, beacon popup, and public essays — always accessible
|
|
if (PUBLIC_PATHS.has(pathname) || pathname.startsWith("/invite/") || pathname.startsWith("/beacon/") || pathname.startsWith("/thoughts")) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
// Unauthenticated → sign-in with return URL
|
|
if (!req.auth) {
|
|
const signIn = new URL("/sign-in", req.url);
|
|
signIn.searchParams.set("callbackUrl", `${pathname}${search}`);
|
|
return NextResponse.redirect(signIn);
|
|
}
|
|
|
|
// Authenticated but no username yet → onboarding
|
|
if (!req.auth.user?.username && pathname !== "/onboarding") {
|
|
return NextResponse.redirect(new URL("/onboarding", req.url));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
});
|
|
|
|
export const config = {
|
|
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
};
|