You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.2 KiB

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.withProtectedRoutesHandler = void 0;
const adminjs_1 = require("adminjs");
const withProtectedRoutesHandler = (fastifyApp, admin) => {
const { rootPath } = admin.options;
fastifyApp.addHook('preHandler', async (request, reply) => {
if (adminjs_1.Router.assets.find((asset) => request.url.match(asset.path))) {
return;
}
else if (!request.url.startsWith(rootPath) ||
request.session.get('adminUser') ||
// these routes don't need authentication
request.url.startsWith(admin.options.loginPath) ||
request.url.startsWith(admin.options.logoutPath)) {
return;
}
else {
// If the redirection is caused by API call to some action just redirect to resource
const [redirectTo] = request.url.split('/actions');
request.session.redirectTo = redirectTo.includes(`${rootPath}/api`)
? rootPath
: redirectTo;
reply.redirect(admin.options.loginPath);
}
});
};
exports.withProtectedRoutesHandler = withProtectedRoutesHandler;