Skip to content

Commit

Permalink
Correct middleware regex patterns, resolve missing key warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Aug 8, 2024
1 parent cd6d120 commit 26f3a1d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 1 addition & 2 deletions ui/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { getKafkaClusters } from "@/api/kafka/actions";
import { ClusterList } from "@/api/kafka/schema";
import { makeOauthTokenProvider } from "@/app/api/auth/[...nextauth]/oauth-token";
import { logger } from "@/utils/logger";
import NextAuth, { AuthOptions } from "next-auth";
import { Provider } from "next-auth/providers/index";
import { NextRequest, NextResponse } from "next/server";
import { makeAnonymous } from "./anonymous";
import { makeOauthProvider } from "./keycloak";
import { makeOauthTokenProvider } from "./oauth-token";
import { makeScramShaProvider } from "./scram";

const log = logger.child({ module: "auth" });
Expand Down
8 changes: 4 additions & 4 deletions ui/components/ClustersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export function ClustersTable({
renderHeader={({ column, Th }) => {
switch (column) {
case "name":
return <Th width={25}>{t("ClustersTable.name")}</Th>;
return <Th key="name_header" width={25}>{t("ClustersTable.name")}</Th>;
case "version":
return <Th>{t("ClustersTable.kafka_version")}</Th>;
return <Th key="version_header">{t("ClustersTable.kafka_version")}</Th>;
case "namespace":
return <Th>{t("ClustersTable.project")}</Th>;
return <Th key="namespace_header">{t("ClustersTable.project")}</Th>;
case "login":
return <Th modifier={"fitContent"} />;
return <Th key="login_header" modifier={"fitContent"} aria-label="Login buttons" />;
}
}}
renderCell={({ key, column, row, Td }) => {
Expand Down
16 changes: 12 additions & 4 deletions ui/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import withAuth from "next-auth/middleware";
import createIntlMiddleware from "next-intl/middleware";
import { NextRequest, NextResponse } from "next/server";

const publicPages = ["/kafka/\\d/login", "/cluster", "/"];
const protectedPages = ["/kafka/\\d/.*"];
import { logger } from "@/utils/logger";

const log = logger.child({ module: "middleware" });

const publicPages = ["/kafka/[^/]+/login", "/cluster", "/"];
const protectedPages = ["/kafka/[^/]+/.*"];

const intlMiddleware = createIntlMiddleware({
// A list of all locales that are supported
Expand Down Expand Up @@ -47,14 +51,18 @@ const protectedPathnameRegex = RegExp(
);

export default async function middleware(req: NextRequest) {
const isPublicPage = publicPathnameRegex.test(req.nextUrl.pathname);
const isProtectedPage = protectedPathnameRegex.test(req.nextUrl.pathname);
const requestPath = req.nextUrl.pathname;
const isPublicPage = publicPathnameRegex.test(requestPath);
const isProtectedPage = protectedPathnameRegex.test(requestPath);

if (isPublicPage) {
log.trace({ requestPath: requestPath }, "public page");
return intlMiddleware(req);
} else if (isProtectedPage) {
log.trace({ requestPath: requestPath }, "protected page");
return (authMiddleware as any)(req);
} else {
log.debug({ requestPath: requestPath, publicPathnameRegex: publicPathnameRegex, protectedPathnameRegex: protectedPathnameRegex }, "neither public nor protected!");
return NextResponse.redirect(new URL("/", req.url));
}
}
Expand Down

0 comments on commit 26f3a1d

Please sign in to comment.