This is a boilerplate Express application with TypeScript and Mongoose, designed as a starter for demonstrating multi-tenant Role-Based Access Control (RBAC) in MongoDB applications.
project-root/
│
├── src/
│ ├── models/ # Mongoose data models
│ ├── routes/ # Express route definitions
│ ├── controllers/ # Request handling logic
│ ├── middleware/ # Express middleware
│ └── index.ts # Main application entry point
│
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── .env # Environment variables
- Node.js (v18+ recommended)
- npm or yarn
- MongoDB (local or MongoDB Atlas)
-
Clone the repository:
git clone https://github.com/miracleonyenma/permit-mongo-express-app.git cd permit-mongo-express-app -
Install dependencies:
npm install
-
Create a
.envfile in the project root:PORT=3000 MONGODB_URI=mongodb://localhost:27017/yourdbname JWT_SECRET=your_secret_key_here
The application includes a robust authentication system using JSON Web Tokens (JWT):
- User Registration
- User Login
- JWT-based Authentication
- Protected Routes
-
Endpoint:
POST /api/auth/register -
Payload:
{ "username": "johndoe", "email": "john@example.com", "password": "securepassword" } -
Response: Returns user details and JWT token
-
Endpoint:
POST /api/auth/login -
Payload:
{ "email": "john@example.com", "password": "securepassword" } -
Response: Returns user details and JWT token
-
After login, include the token in the
Authorizationheader:Authorization: Bearer <your_jwt_token>
npm run devThis will start the server with nodemon, enabling hot-reloading during development.
npm run build # Compile TypeScript
npm start # Run the compiled JavaScriptnpm run dev: Start development server with hot-reloadingnpm run build: Compile TypeScript to JavaScriptnpm start: Run the production buildnpm run lint: Run ESLint for code quality checks
The tsconfig.json is configured to:
- Target ES2020
- Use CommonJS modules
- Output compiled files to
./dist - Enable strict type checking
Configured to watch TypeScript files in the src directory and restart the server on changes.
- Express: Web application framework
- Mongoose: MongoDB object modeling tool
- JsonWebToken: Authentication token generation
- Bcrypt: Password hashing
- Dotenv: Environment variable management
- TypeScript
- Nodemon
- ESLint with TypeScript support
This boilerplate is part of a tutorial on implementing multi-tenant RBAC. Upcoming features include:
- Role-based access control
- Multi-tenant user management
- Permission filtering
- Integration with Permit.io
- Passwords are hashed using bcrypt
- JWT tokens are used for stateless authentication
- Routes are protected with middleware
- Sensitive information is managed through environment variables
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the ISC License. See LICENSE for more information.
Miracle Onyenma - @miracleio
Project Link: https://github.com/miracleonyenma/permit-mongo-express-app