mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
export const config = {
|
|
matcher: [
|
|
/*
|
|
* Match all paths except for:
|
|
* 1. /api routes
|
|
* 2. /_next (Next.js internals)
|
|
* 3. /fonts (inside /public)
|
|
* 4. /examples (inside /public)
|
|
* 5. all root files inside /public (e.g. /favicon.ico)
|
|
*/
|
|
"/((?!api|_next|fonts|login|signup|examples|[\\w-]+\\.\\w+).*)",
|
|
],
|
|
};
|
|
|
|
|
|
export default function middleware(req: NextRequest) {
|
|
const LEARNHOUSE_DOMAIN = process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN;
|
|
const url = req.nextUrl;
|
|
const isSelfHosted = process.env.NEXT_PUBLIC_LEARNHOUSE_SELF_HOSTED === "true" ? true : false
|
|
const hostname = req.headers.get("host") || "learnhouse.app";
|
|
const defaultOrg = isSelfHosted ? process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG : null;
|
|
let currentHost = hostname.replace(`.${LEARNHOUSE_DOMAIN}`, "");
|
|
|
|
if (!isSelfHosted && currentHost === LEARNHOUSE_DOMAIN && !url.pathname.startsWith("/organizations")) {
|
|
// Redirect to error page if not self-hosted and on localhost
|
|
const errorUrl = "/error";
|
|
return NextResponse.redirect(errorUrl, { status: 302 });
|
|
}
|
|
|
|
if (url.pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) {
|
|
url.pathname = `/_editor${url.pathname}`;
|
|
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
|
|
}
|
|
|
|
if (url.pathname.startsWith("/organizations")) {
|
|
if (!isSelfHosted) {
|
|
currentHost = "";
|
|
}
|
|
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`).replace("localhost:3000", "");
|
|
|
|
return NextResponse.rewrite(url);
|
|
}
|
|
|
|
if (isSelfHosted) {
|
|
currentHost = getDefaultOrg() || currentHost;
|
|
}
|
|
|
|
url.pathname = `/_orgs/${currentHost}${url.pathname}`;
|
|
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
|
|
}
|