feat: improve middleware implementation

This commit is contained in:
swve 2023-03-24 23:58:09 +01:00
parent ae280b2cfb
commit aa90283fe7
2 changed files with 36 additions and 29 deletions

View file

@ -1,3 +1,4 @@
import { getDefaultOrg, getSelfHostedOption } from "@services/config/config";
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
export const config = { export const config = {
@ -16,41 +17,30 @@ export const config = {
export default function middleware(req: NextRequest) { export default function middleware(req: NextRequest) {
const url = req.nextUrl; const url = req.nextUrl;
const isSelfHosted = getSelfHostedOption();
// Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000)
const hostname = req.headers.get("host") || "learnhouse.app"; const hostname = req.headers.get("host") || "learnhouse.app";
let currentHost = hostname.replace(".localhost:3000", "");
if (!isSelfHosted && currentHost === ("localhost:3000")) {
// Redirect to error page if not self-hosted and on localhost
const errorUrl = "/error";
return NextResponse.redirect(errorUrl, { status: 302 });
}
/* You have to replace ".vercel.pub" with your own domain if you deploy this example under your domain.
You can also use wildcard subdomains on .vercel.app links that are associated with your Vercel team slug
in this case, our team slug is "platformize", thus *.platformize.vercel.app works. Do note that you'll
still need to add "*.platformize.vercel.app" as a wildcard domain on your Vercel dashboard. */
let currentHost =
process.env.NODE_ENV === "production" && process.env.VERCEL === "1"
? hostname.replace(`.vercel.pub`, "").replace(`.platformize.vercel.app`, "")
: hostname.replace(`.localhost:3000`, "");
/* Editor route */
if (url.pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) { if (url.pathname.match(/^\/course\/[^/]+\/activity\/[^/]+\/edit$/)) {
url.pathname = `/_editor${url.pathname}`; url.pathname = `/_editor${url.pathname}`;
console.log("editor route", url.pathname);
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } }); return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
} }
/* Organizations route */
if (url.pathname.startsWith("/organizations")) { if (url.pathname.startsWith("/organizations")) {
url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`); url.pathname = url.pathname.replace("/organizations", `/organizations${currentHost}`).replace("localhost:3000", "");
// remove localhost:3000 from url
url.pathname = url.pathname.replace(`localhost:3000`, "");
return NextResponse.rewrite(url); return NextResponse.rewrite(url);
} }
console.log("currentHost", url); if (isSelfHosted) {
currentHost = getDefaultOrg() || currentHost;
}
// rewrite everything else to `/_sites/[site] dynamic route
url.pathname = `/_orgs/${currentHost}${url.pathname}`; url.pathname = `/_orgs/${currentHost}${url.pathname}`;
return NextResponse.rewrite(url, { headers: { orgslug: currentHost } });
return NextResponse.rewrite(url, { headers: { olgslug: currentHost } });
} }

View file

@ -5,14 +5,31 @@ export const getAPIUrl = () => LEARNHOUSE_API_URL;
export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL; export const getBackendUrl = () => LEARNHOUSE_BACKEND_URL;
export const getSelfHostedOption = () => false;
export const getUriWithOrg = (orgslug: string, path: string) => { export const getUriWithOrg = (orgslug: string, path: string) => {
const selfHosted = getSelfHostedOption();
if (selfHosted) {
return `http://localhost:3000${path}`;
}
return `http://${orgslug}.localhost:3000${path}`; return `http://${orgslug}.localhost:3000${path}`;
}; };
export const getOrgFromUri = () => { export const getOrgFromUri = () => {
const hostname = window.location.hostname; const selfHosted = getSelfHostedOption();
// get the orgslug from the hostname if (selfHosted) {
const orgslug = hostname.split(".")[0]; getDefaultOrg();
return orgslug; } else {
const hostname = window.location.hostname;
// get the orgslug from the hostname
const orgslug = hostname.split(".")[0];
return orgslug;
}
};
export const getDefaultOrg = () => {
const selfHosted = getSelfHostedOption();
if (selfHosted) {
return "test";
}
}; };