An open API service indexing awesome lists of open source software.

https://github.com/ctrly4sh/auth-rbac-rest-api

Role Base Access Control Rest API implementation in Express , Typescript, Prisma and Neon DB ( serverless postgres) with Bun.
https://github.com/ctrly4sh/auth-rbac-rest-api

authentication backend nodejs security

Last synced: 9 months ago
JSON representation

Role Base Access Control Rest API implementation in Express , Typescript, Prisma and Neon DB ( serverless postgres) with Bun.

Awesome Lists containing this project

README

          

# Auth RBAC REST API

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.

Design
RBAC Design

## ๐Ÿš€ Features

- **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

## ๐Ÿ› ๏ธ Tech Stack

- **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

## ๐Ÿ“ฆ Prerequisites

- Node.js (v18+)
- Bun (v1.0.0+)
- PostgreSQL (v14+)
- Git

## ๐Ÿš€ Getting Started

1. **Clone the repository**
```bash
git clone https://github.com/ctrly4sh/Auth-RBAC-Rest-API.git
cd Auth-RBAC-Rest-API
```

2. **Install dependencies**
```bash
bun install
```

3. **Set up environment variables**
Create a `.env` file in the root directory and add the following variables:
```env
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
```

4. **Run database migrations**
```bash
npx prisma migrate dev --name init
```

5. **Start the development server**
```bash
bun run dev
```

Or with hot-reload:
```bash
bun run watch
```

## ๐Ÿ“š API Documentation

API documentation is available in the Postman collection file: `postman-collection.json`

### Available Endpoints

- `POST /api/auth/register` - Register a new user
- `POST /api/auth/login` - User login
- `POST /api/auth/refresh-token` - Refresh access token
- `GET /api/users/me` - Get current user profile (protected)
- `GET /api/users` - Get all users (admin only)

## ๐Ÿงช Testing

To run tests:
```bash
bun test
```

## ๐Ÿ—๏ธ Project Structure

```
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
```

## ๐Ÿ”’ Security

- 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

## ๐Ÿค Contributing

1. Fork the project
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

Official Documentation and Resources
- [Express.js](https://expressjs.com/)
- [Prisma](https://www.prisma.io/)
- [JWT](https://jwt.io/)
- [Bun](https://bun.sh/)
- [Excalidraw](https://excalidraw.com/)