UNPKG

@vortex-js/core

Version:

A simple and powerful role-based access control (RBAC) middleware for Express.js, designed to be easy to use and integrate with your existing applications. It provides a flexible way to manage user permissions and roles, making it ideal for building secur

53 lines (52 loc) 1.91 kB
import PostmanController from "./PotmanController"; import { findRequestRai } from "../lib/rais/middlewares/findRequestRai"; import { isAuthorized } from "../lib/rais/middlewares/isAuthorized"; import { InitializeCreatingRAIs } from "../lib/rais"; class AppWrapper extends PostmanController { constructor(config) { super(config.routes, config.postman); this.app = config.app; this.routes = config.routes; this.roles = config.roles ? config.roles : []; } getExpressApp(middlewares = [], postMiddlewares = []) { /** * Seed RAIs & roles in the app.locals * This is used to find the RAI for the current request * and to check if the user is authorized to access the route */ const { rais } = InitializeCreatingRAIs(this.routes); this.app.locals.roles = this.roles?.map((role) => { return { _id: role, name: role, }; }); this.app.locals.rais = rais; this.app.use(findRequestRai, isAuthorized); /** * Register middlewares * These middlewares will be applied to all routes * * we are doing this because if you register any middlewares * after the routes, they will not be executed */ if (middlewares && middlewares.length > 0) { middlewares.forEach((middleware) => { this.app.use(middleware); }); } this.app.use(this.routes.buildRouter()); /** * Register post middlewares * These middlewares will be applied after the routes */ if (postMiddlewares && postMiddlewares.length > 0) { postMiddlewares.forEach((middleware) => { this.app.use(middleware); }); } return this.app; } } export default AppWrapper;