Files
otunes/apps/web/proxy.server.ts
T
joshuafhigginsandClaude Opus 5.5 Verified 3c1191a243 feat(web): build the UI as a Next.js static export
In-app links move to /album?id=, /artist?id= and /playlist?id= so every
page can prerender. Server-only files use a .server.ts(x) suffix that
`pnpm build:export` leaves out: route handlers, the proxy, the desktop
server picker, and the /album/<id> share pages with link-preview metadata.
Exports use client-only Convex Auth and redirect in the browser.

Sign-up now checks the setting in the browser. The sign-in link always
shows, and a disabled server replaces the sign-up form with a notice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 00:15:10 -05:00

60 lines
1.5 KiB
TypeScript

import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server"
import {
NextResponse,
type NextRequest,
type NextFetchEvent,
} from "next/server"
import { getConvexServerUrl } from "@/lib/convex-server"
const isAuthPage = createRouteMatcher(["/signin", "/signup"])
const isPublic = createRouteMatcher([
"/api/album/:id/cover",
"/api/artist/:id/cover",
"/api/playlist/:id/cover",
// Mobile clients load avatars without the web auth cookie.
"/api/user/:id/avatar",
"/album",
"/album/:id",
"/artist",
"/artist/:id",
"/playlist",
"/playlist/:id",
])
const authMiddleware = convexAuthNextjsMiddleware(
async (request, { convexAuth }) => {
if (isPublic(request)) {
return
}
const authenticated = await convexAuth.isAuthenticated()
if (isAuthPage(request) && authenticated) {
return nextjsMiddlewareRedirect(request, "/")
}
if (!isAuthPage(request) && !authenticated) {
return nextjsMiddlewareRedirect(request, "/signin")
}
},
{ convexUrl: getConvexServerUrl() }
)
export default function proxy(request: NextRequest, event: NextFetchEvent) {
if (process.env.OTUNES_DESKTOP === "1") {
if (request.nextUrl.pathname === "/desktop") return NextResponse.next()
if (!process.env.PUBLIC_CONVEX_URL) {
return NextResponse.redirect(new URL("/desktop", request.url))
}
}
return authMiddleware(request, event)
}
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
}