feat: improve front middleware code

This commit is contained in:
swve 2023-05-17 18:57:37 +00:00
parent 3c2a3e6dea
commit 328cde854a
2 changed files with 34 additions and 36 deletions

View file

@ -1,5 +1,6 @@
import { LEARNHOUSE_DOMAIN, getDefaultOrg, getSelfHostedOption } from "./services/config/config"; import { LEARNHOUSE_DOMAIN, getDefaultOrg, isMultiOrgModeEnabled } from "./services/config/config";
import { NextRequest, NextResponse } from "next/server"; import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export const config = { export const config = {
matcher: [ matcher: [
@ -16,35 +17,33 @@ export const config = {
}; };
export default function middleware(req: NextRequest) { export default function middleware(req: NextRequest) {
const url = req.nextUrl; // Get initial data
const isSelfHosted = getSelfHostedOption(); const hosting_mode = isMultiOrgModeEnabled() ? "multi" : "single";
const hostname = req.headers.get("host") || "learnhouse.app"; const default_org = getDefaultOrg();
let currentHost = hostname.replace(`.${LEARNHOUSE_DOMAIN}`, ""); const pathname = req.nextUrl.pathname;
const fullhost = req.headers ? req.headers.get("host") : "";
if (!isSelfHosted && currentHost === "localhost:3000" && !url.pathname.startsWith("/organizations")) { // Organizations & Global settings
// Redirect to error page if not self-hosted and on localhost if (pathname.startsWith("/organizations")) {
const errorUrl = "/error"; return NextResponse.rewrite(new URL("/organizations", req.url));
return NextResponse.redirect(errorUrl, { status: 302 });
} }
if (url.pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) { // Dynamic Pages Editor
url.pathname = `/_editor${url.pathname}`; if (pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) {
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } }); return NextResponse.rewrite(new URL(`/_editor${pathname}`, req.url));
} }
if (url.pathname.startsWith("/organizations")) { // Multi Organization Mode
if (!isSelfHosted) { if (hosting_mode === "multi") {
currentHost = ""; // Get the organization slug from the URL
} const orgslug = fullhost ? fullhost.replace(`.${LEARNHOUSE_DOMAIN}`, "") : default_org;
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`).replace("localhost:3000", ""); return NextResponse.rewrite(new URL(`/_orgs/${orgslug}${pathname}`, req.url));
return NextResponse.rewrite(url);
} }
if (isSelfHosted) { // Single Organization Mode
currentHost = getDefaultOrg() || currentHost; if (hosting_mode === "single") {
// Get the default organization slug
const orgslug = default_org;
return NextResponse.rewrite(new URL(`/_orgs/${orgslug}${pathname}`, req.url));
} }
url.pathname = `/_orgs/${currentHost}${url.pathname}`;
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
} }

View file

@ -5,19 +5,21 @@ export const LEARNHOUSE_DOMAIN = process.env.NEXT_PUBLIC_LEARNHOUSE_DOMAIN;
export const getAPIUrl = () => LEARNHOUSE_API_URL; export const getAPIUrl = () => LEARNHOUSE_API_URL;
export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL; export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL;
export const getSelfHostedOption = () => (process.env.NEXT_PUBLIC_LEARNHOUSE_SELF_HOSTED === "true" ? true : false);
// Multi Organization Mode
export const isMultiOrgModeEnabled = () => (process.env.NEXT_PUBLIC_LEARNHOUSE_MULTI_ORG === "true" ? true : false);
export const getUriWithOrg = (orgslug: string, path: string) => { export const getUriWithOrg = (orgslug: string, path: string) => {
const selfHosted = getSelfHostedOption(); const multi_org = isMultiOrgModeEnabled();
if (selfHosted) { if (multi_org) {
return `${LEARNHOUSE_DOMAIN}${path}`;
}
return `${LEARNHOUSE_HTTP_PROTOCOL}${orgslug}.${LEARNHOUSE_DOMAIN}${path}`; return `${LEARNHOUSE_HTTP_PROTOCOL}${orgslug}.${LEARNHOUSE_DOMAIN}${path}`;
}
return `${LEARNHOUSE_HTTP_PROTOCOL}${LEARNHOUSE_DOMAIN}${path}`;
}; };
export const getOrgFromUri = () => { export const getOrgFromUri = () => {
const selfHosted = getSelfHostedOption(); const multi_org = isMultiOrgModeEnabled();
if (selfHosted) { if (multi_org) {
getDefaultOrg(); getDefaultOrg();
} else { } else {
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
@ -29,8 +31,5 @@ export const getOrgFromUri = () => {
}; };
export const getDefaultOrg = () => { export const getDefaultOrg = () => {
const selfHosted = getSelfHostedOption();
if (selfHosted) {
return process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG; return process.env.NEXT_PUBLIC_LEARNHOUSE_DEFAULT_ORG;
}
}; };