A secure and scalable Role-Based Access Control (RBAC) REST API built with Express.js, TypeScript, and Prisma. This API provides authentication and authorization services with JWT tokens and refresh tokens.
-
User Authentication
- Secure registration and login with email/password
- JWT-based authentication
- Refresh token mechanism
- Rate limiting for security
-
Role-Based Access Control (RBAC)
- Predefined user roles (ADMIN, USER)
- Middleware for role-based route protection
- Extensible permission system
-
Security
- Password hashing with bcrypt
- Helmet for security headers
- CORS protection
- Environment-based configuration
-
Database
- PostgreSQL with Prisma ORM
- Database migrations
- Type-safe database queries
- Runtime: Node.js with Bun
- Language: TypeScript
- Framework: Express.js
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JWT
- Security: Helmet, bcrypt, express-rate-limit
- Validation: Express Validator
- Node.js (v18+)
- Bun (v1.0.0+)
- PostgreSQL (v14+)
- Git
-
Clone the repository
git clone https://github.com/ctrly4sh/Auth-RBAC-Rest-API.git cd Auth-RBAC-Rest-API
-
Install dependencies
bun install
-
Set up environment variables Create a
.env
file in the root directory and add the following variables:DATABASE_URL="postgresql://user:password@localhost:5432/auth_rbac?schema=public" JWT_SECRET=your_jwt_secret_key JWT_REFRESH_SECRET=your_refresh_token_secret PORT=3000 NODE_ENV=development
-
Run database migrations
npx prisma migrate dev --name init
-
Start the development server
bun run dev
Or with hot-reload:
bun run watch
API documentation is available in the Postman collection file: postman-collection.json
POST /api/auth/register
- Register a new userPOST /api/auth/login
- User loginPOST /api/auth/refresh-token
- Refresh access tokenGET /api/users/me
- Get current user profile (protected)GET /api/users
- Get all users (admin only)
To run tests:
bun test
src/
├── config/ # Configuration files
├── controllers/ # Route controllers
├── middlewares/ # Custom middlewares
├── routes/ # Route definitions
├── services/ # Business logic
├── types/ # TypeScript type definitions
├── app.ts # Express application setup
└── server.ts # Server entry point
- All passwords are hashed using bcrypt
- JWT tokens for stateless authentication
- Rate limiting to prevent brute force attacks
- Security headers with Helmet
- Environment variables for sensitive data
- Fork the project
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
Official Documentation and Resources