Skip to content

Commit

Permalink
fix: fix isAdminRoute helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Feb 21, 2022
1 parent d931569 commit 561c225
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/authentication/protected-routes.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ export const withProtectedRoutesHandler = (
router: Router,
admin: AdminJS
): void => {
const { rootPath } = admin.options;
const { rootPath, loginPath, logoutPath } = admin.options;

router.use((req, res, next) => {
if (isAdminAsset(req.originalUrl)) {
next();
} else if (
req.session.adminUser ||
// these routes doesn't need authentication
req.originalUrl.startsWith(admin.options.loginPath) ||
req.originalUrl.startsWith(admin.options.logoutPath)
req.originalUrl.startsWith(loginPath) ||
req.originalUrl.startsWith(logoutPath)
) {
next();
} else if (isAdminRoute(req.originalUrl, rootPath)) {
Expand All @@ -30,23 +30,32 @@ export const withProtectedRoutesHandler = (
if (err) {
next(err);
}
res.redirect(admin.options.loginPath);
res.redirect(loginPath);
});
} else {
next();
}
});
};

export const isAdminRoute = (url: string, adminRootUrl: string): boolean => {
export const isAdminRoute = (url: string, adminRootPath: string): boolean => {
const adminRoutes = AdminRouter.routes
.map((route) => convertToExpressRoute(route.path))
.filter((route) => route !== "");
const isAdminRootUrl = url === adminRootUrl;

let urlWithoutRootPath = url;
if (adminRootPath !== '/') {
urlWithoutRootPath = url.replace(adminRootPath, '');
if (!urlWithoutRootPath.startsWith('/')) {
urlWithoutRootPath = `/${urlWithoutRootPath}`
}
}

const isAdminRootUrl = url === adminRootPath || urlWithoutRootPath === '/';

return (
isAdminRootUrl ||
!!adminRoutes.find((route) => pathToRegexp(route).test(url))
!!adminRoutes.find((route) => pathToRegexp(route).test(urlWithoutRootPath))
);
};

Expand Down
29 changes: 28 additions & 1 deletion test/protected-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isAdminRoute } from "../src/authentication/protected-routes.handler";

describe("Protected routes", () => {
describe("#isAdminRoute", () => {
it("should detect admin routes", () => {
it("should detect admin routes when root path is /", () => {
const adminRoutes = [
"/",
"/resources/someResource",
Expand All @@ -29,6 +29,33 @@ describe("Protected routes", () => {
});
});

it("should detect admin routes when root path is not /", () => {
const adminRoutes = [
"/admin",
"/admin/resources/someResource",
"/admin/api/resources/someResource/search/searchQuery",
"/admin/resources/someResource/actions/someAction",
"/admin/api/resources/someResource/actions/someAction",
"/admin/api/resources/someResource/actions/someAction/searchQuery",
"/admin/api/resources/someResource/actions/someAction",
"/admin/resources/someResource/records/someRecordId/someAction",
"/admin/api/resources/someResource/records/someRecordId/someAction",
"/admin/api/resources/someResource/records/someRecordId/someAction",
"/admin/resources/someResource/bulk/someAction",
"/admin/api/resources/someResource/bulk/someAction",
"/admin/api/resources/someResource/bulk/someAction",
"/admin/api/resources/someResource/search/",
"/admin/api/dashboard",
"/admin/pages/somePage",
"/admin/api/pages/somePage",
"/admin/api/pages/somePage",
];

adminRoutes.forEach((route) => {
expect(isAdminRoute(route, "/admin")).toBeTruthy();
});
});

it("should detect non-admin routes", () => {
expect(isAdminRoute("/api/my-endpoint", "/")).toBeFalsy();
});
Expand Down

0 comments on commit 561c225

Please sign in to comment.