Skip to content

Commit 9daf9e9

Browse files
committed
fix: fix protected routes when root path is different than /
1 parent 297d681 commit 9daf9e9

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/authentication/protected-routes.handler.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@ export const withProtectedRoutesHandler = (
3333
});
3434
};
3535

36-
export const isAdminRoute = (url: string, adminRootPath: string): boolean => {
36+
export const isAdminRoute = (
37+
originalUrl: string,
38+
adminRootPath: string
39+
): boolean => {
3740
const adminRoutes = AdminRouter.routes
3841
.map((route) => convertToExpressRoute(route.path))
3942
.filter((route) => route !== "");
4043

41-
let urlWithoutAdminRootPath = url.split("?")[0];
44+
let urlWithoutAdminRootPath = originalUrl.split("?")[0];
4245
if (adminRootPath !== "/") {
43-
urlWithoutAdminRootPath = url.replace(adminRootPath, "");
46+
urlWithoutAdminRootPath = urlWithoutAdminRootPath.replace(
47+
adminRootPath,
48+
""
49+
);
4450
if (!urlWithoutAdminRootPath.startsWith("/")) {
4551
urlWithoutAdminRootPath = `/${urlWithoutAdminRootPath}`;
4652
}
4753
}
4854

49-
const isAdminRootUrl = url === adminRootPath;
55+
const isAdminRootUrl = originalUrl === adminRootPath;
56+
const isUrlUnderRootPath = originalUrl.startsWith(adminRootPath);
5057

5158
return (
5259
isAdminRootUrl ||
53-
adminRoutes.some((route) =>
60+
(adminRoutes.some((route) =>
5461
pathToRegexp(route).test(urlWithoutAdminRootPath)
55-
)
62+
) &&
63+
isUrlUnderRootPath)
5664
);
5765
};
5866

test/protected-routes.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,22 @@ describe("Protected routes", () => {
5757
});
5858

5959
it("should detect admin routes when query params are included", () => {
60-
const route = "/resources/list?filters.someFilter=123";
60+
const route =
61+
"/resources/someResource/actions/list?filters.someFilter=123";
6162

6263
expect(isAdminRoute(route, "/")).toBeTruthy();
6364
});
6465

66+
it("should detect admin routes when query params are included and root path is not /", () => {
67+
const route =
68+
"/admin/resources/someResource/actions/list?filters.someFilter=123";
69+
70+
expect(isAdminRoute(route, "/admin")).toBeTruthy();
71+
});
72+
6573
it("should not detect admin routes when query params are included but root is different", () => {
66-
const route = "/resources/list?filters.someFilter=123";
74+
const route =
75+
"/resources/someResource/actions/list?filters.someFilter=123";
6776

6877
expect(isAdminRoute(route, "/admin")).toBeFalsy();
6978
});

0 commit comments

Comments
 (0)