41 lines
1000 B
TypeScript
41 lines
1000 B
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([
|
|
"/api/album/:id/cover",
|
|
"/api/artist/:id/cover",
|
|
"/api/playlist/:id/cover",
|
|
"/album/:id",
|
|
"/artist/:id",
|
|
"/library/artists/:id",
|
|
"/playlist/:id",
|
|
])
|
|
|
|
export default 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 const config = {
|
|
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
|
|
}
|