link-stack/apps/link/middleware.ts

82 lines
3 KiB
TypeScript
Raw Permalink Normal View History

2023-05-30 09:05:40 +00:00
import { NextResponse } from 'next/server';
2023-06-05 14:48:23 +00:00
import { withAuth, NextRequestWithAuth } from "next-auth/middleware";
2023-05-30 09:05:40 +00:00
2023-06-14 06:33:06 +00:00
const rewriteURL = (request: NextRequestWithAuth, originBaseURL: string, destinationBaseURL: string, headers: any = {}) => {
2023-05-30 09:05:40 +00:00
const destinationURL = request.url.replace(originBaseURL, destinationBaseURL);
2023-08-25 07:11:33 +00:00
2023-05-30 09:05:40 +00:00
console.log(`Rewriting ${request.url} to ${destinationURL}`);
2023-06-05 15:00:46 +00:00
const requestHeaders = new Headers(request.headers);
for (const [key, value] of Object.entries(headers)) {
2023-08-25 07:11:33 +00:00
requestHeaders.set(key, value as string);
2023-06-05 15:00:46 +00:00
}
2023-06-05 14:48:23 +00:00
2023-06-05 15:00:46 +00:00
requestHeaders.delete('connection');
2023-06-05 14:48:23 +00:00
return NextResponse.rewrite(new URL(destinationURL), { request: { headers: requestHeaders } });
2023-05-30 09:05:40 +00:00
};
2023-06-05 14:48:23 +00:00
const checkRewrites = async (request: NextRequestWithAuth) => {
2023-06-05 15:00:46 +00:00
const linkBaseURL = process.env.LINK_URL ?? "http://localhost:3000";
const zammadURL = process.env.ZAMMAD_URL ?? "http://zammad-nginx:8080";
2023-08-25 07:11:33 +00:00
const metamigoURL = process.env.METAMIGO_URL ?? "http://metamigo-api:3000";
2023-07-21 12:26:02 +00:00
const labelStudioURL = process.env.LABEL_STUDIO_URL ?? "http://label-studio:8080";
2023-08-25 07:11:33 +00:00
const { token } = request.nextauth;
const headers = { 'X-Forwarded-User': token?.email?.toLowerCase() };
2023-06-05 14:48:23 +00:00
2023-08-25 07:11:33 +00:00
if (request.nextUrl.pathname.startsWith('/metamigo')) {
return rewriteURL(request, `${linkBaseURL}/metamigo`, metamigoURL);
} else if (request.nextUrl.pathname.startsWith('/label-studio')) {
return rewriteURL(request, `${linkBaseURL}/label-studio`, labelStudioURL);
} else if (request.nextUrl.pathname.startsWith('/zammad')) {
return rewriteURL(request, `${linkBaseURL}/zammad`, zammadURL, headers);
2023-08-25 07:11:33 +00:00
} else if (request.nextUrl.pathname.startsWith('/auth/sso') || request.nextUrl.pathname.startsWith('/assets')) {
return rewriteURL(request, linkBaseURL, zammadURL, headers);
} else if (request.nextUrl.pathname.startsWith('/proxy/api') || request.nextUrl.pathname.startsWith('/proxy/assets')) {
2023-06-05 14:48:23 +00:00
return rewriteURL(request, `${linkBaseURL}/proxy`, zammadURL);
2023-09-12 14:33:37 +02:00
} else if (request.nextUrl.pathname.startsWith('/api/v1') || request.nextUrl.pathname.startsWith('/auth/sso') || request.nextUrl.pathname.startsWith('/mobile')) {
return rewriteURL(request, linkBaseURL, zammadURL, headers);
2023-05-30 09:05:40 +00:00
}
2023-08-25 07:11:33 +00:00
return NextResponse.next();
2023-05-30 09:05:40 +00:00
};
2023-05-24 20:27:57 +00:00
export default withAuth(
2023-05-30 09:05:40 +00:00
checkRewrites,
2023-05-24 20:27:57 +00:00
{
pages: {
signIn: `/login`,
},
callbacks: {
authorized: ({ token, req }) => {
const {
url,
} = req;
2023-09-06 16:42:52 +02:00
const noAuthPaths = ["/login", "/api/v1"];
2023-05-24 20:27:57 +00:00
const parsedURL = new URL(url);
2023-08-25 07:11:33 +00:00
const path = parsedURL.pathname;
console.log({ p: parsedURL.pathname });
if (noAuthPaths.some((p: string) => path.startsWith(p))) {
console.log({ a: "no auth" });
2023-05-24 20:27:57 +00:00
return true;
}
2023-08-25 07:11:33 +00:00
const roles: any = token?.roles ?? [];
2023-08-28 14:36:34 +02:00
if (roles.includes("admin") || roles.includes("agent") || process.env.SETUP_MODE === "true") {
return true;
}
2023-05-24 20:27:57 +00:00
return false;
},
}
}
);
2023-08-25 07:11:33 +00:00
export const config = {
matcher: [
'/((?!ws|wss|_next/static|_next/image|favicon.ico).*)',
2023-08-25 07:11:33 +00:00
],
};