Skip to content

Commit 561c225

Browse files
committed
fix: fix isAdminRoute helper
1 parent d931569 commit 561c225

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/authentication/protected-routes.handler.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ export const withProtectedRoutesHandler = (
77
router: Router,
88
admin: AdminJS
99
): void => {
10-
const { rootPath } = admin.options;
10+
const { rootPath, loginPath, logoutPath } = admin.options;
1111

1212
router.use((req, res, next) => {
1313
if (isAdminAsset(req.originalUrl)) {
1414
next();
1515
} else if (
1616
req.session.adminUser ||
1717
// these routes doesn't need authentication
18-
req.originalUrl.startsWith(admin.options.loginPath) ||
19-
req.originalUrl.startsWith(admin.options.logoutPath)
18+
req.originalUrl.startsWith(loginPath) ||
19+
req.originalUrl.startsWith(logoutPath)
2020
) {
2121
next();
2222
} else if (isAdminRoute(req.originalUrl, rootPath)) {
@@ -30,23 +30,32 @@ export const withProtectedRoutesHandler = (
3030
if (err) {
3131
next(err);
3232
}
33-
res.redirect(admin.options.loginPath);
33+
res.redirect(loginPath);
3434
});
3535
} else {
3636
next();
3737
}
3838
});
3939
};
4040

41-
export const isAdminRoute = (url: string, adminRootUrl: string): boolean => {
41+
export const isAdminRoute = (url: string, adminRootPath: string): boolean => {
4242
const adminRoutes = AdminRouter.routes
4343
.map((route) => convertToExpressRoute(route.path))
4444
.filter((route) => route !== "");
45-
const isAdminRootUrl = url === adminRootUrl;
45+
46+
let urlWithoutRootPath = url;
47+
if (adminRootPath !== '/') {
48+
urlWithoutRootPath = url.replace(adminRootPath, '');
49+
if (!urlWithoutRootPath.startsWith('/')) {
50+
urlWithoutRootPath = `/${urlWithoutRootPath}`
51+
}
52+
}
53+
54+
const isAdminRootUrl = url === adminRootPath || urlWithoutRootPath === '/';
4655

4756
return (
4857
isAdminRootUrl ||
49-
!!adminRoutes.find((route) => pathToRegexp(route).test(url))
58+
!!adminRoutes.find((route) => pathToRegexp(route).test(urlWithoutRootPath))
5059
);
5160
};
5261

test/protected-routes.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isAdminRoute } from "../src/authentication/protected-routes.handler";
22

33
describe("Protected routes", () => {
44
describe("#isAdminRoute", () => {
5-
it("should detect admin routes", () => {
5+
it("should detect admin routes when root path is /", () => {
66
const adminRoutes = [
77
"/",
88
"/resources/someResource",
@@ -29,6 +29,33 @@ describe("Protected routes", () => {
2929
});
3030
});
3131

32+
it("should detect admin routes when root path is not /", () => {
33+
const adminRoutes = [
34+
"/admin",
35+
"/admin/resources/someResource",
36+
"/admin/api/resources/someResource/search/searchQuery",
37+
"/admin/resources/someResource/actions/someAction",
38+
"/admin/api/resources/someResource/actions/someAction",
39+
"/admin/api/resources/someResource/actions/someAction/searchQuery",
40+
"/admin/api/resources/someResource/actions/someAction",
41+
"/admin/resources/someResource/records/someRecordId/someAction",
42+
"/admin/api/resources/someResource/records/someRecordId/someAction",
43+
"/admin/api/resources/someResource/records/someRecordId/someAction",
44+
"/admin/resources/someResource/bulk/someAction",
45+
"/admin/api/resources/someResource/bulk/someAction",
46+
"/admin/api/resources/someResource/bulk/someAction",
47+
"/admin/api/resources/someResource/search/",
48+
"/admin/api/dashboard",
49+
"/admin/pages/somePage",
50+
"/admin/api/pages/somePage",
51+
"/admin/api/pages/somePage",
52+
];
53+
54+
adminRoutes.forEach((route) => {
55+
expect(isAdminRoute(route, "/admin")).toBeTruthy();
56+
});
57+
});
58+
3259
it("should detect non-admin routes", () => {
3360
expect(isAdminRoute("/api/my-endpoint", "/")).toBeFalsy();
3461
});

0 commit comments

Comments
 (0)