Main's download routes are server-only like the other API routes. They take the session from a `token` parameter, the options POST is allowed cross-origin, and the client fetches songs and covers from the picked server. The service worker matches downloaded covers from any origin, and skips offline page caching in exports since they are served locally. pnpm 12 fails on ignored build scripts, so electron-winstaller's is denied explicitly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import {
|
|
convexAuthNextjsMiddleware,
|
|
createRouteMatcher,
|
|
nextjsMiddlewareRedirect,
|
|
} from "@convex-dev/auth/nextjs/server"
|
|
import { getConvexServerUrl } from "@/lib/convex-server"
|
|
|
|
const isAuthPage = createRouteMatcher(["/signin", "/signup"])
|
|
const isPublic = createRouteMatcher([
|
|
// Song routes check the session themselves, from the cookie or a `token`
|
|
// query parameter (static exports on another origin).
|
|
"/api/song/(.*)",
|
|
"/api/download/options",
|
|
"/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 authMiddleware
|
|
|
|
export const config = {
|
|
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
|
|
}
|