mrmeaow/hono-pg-erp-authorization-demo
ERP grade authorization demo using Hono + PostgreSQL
ERP demo API with advanced authorization
A robust ERP-grade API system built with modern TypeScript, featuring comprehensive Role-Based Access Control (RBAC), Attribute-Based Access Control (ABAC), and Policy-Based Access Control (PBAC).
Important
This example/demo is not a fully implemented application.
It is only intended to demonstrate how to use RBAC, permissions, and policies to build a flexible authorization system with database persistence for dynamic control.
Features
๐ Advanced Authorization System
- Role-Based Access Control (RBAC)
- Attribute-Based Access Control (ABAC)
- Policy-Based Access Control (PBAC)
- Direct user permissions
- Dynamic policy evaluation with conditions
๐๏ธ Modern Tech Stack
- Backend: Hono.js (lightweight, fast web framework)
- Database: PostgreSQL with Drizzle ORM
- Runtime: Node.js with TypeScript
- Testing: Vitest
- Authentication: JWT-based auth system
๐ ERP Functionality
- Invoice management system
- User management
- Role and permission management
- Policy-based access control
Project Structure
.
โโโ drizzle/ # Database migrations
โโโ src/
โ โโโ api/ # API route handlers
โ โ โโโ auth.ts # Authentication endpoints
โ โ โโโ invoices.ts # Invoice management
โ โ โโโ rbac.ts # RBAC management
โ โโโ db/
โ โ โโโ config.ts # Database configuration
โ โ โโโ schema.ts # Database schema definitions
โ โ โโโ seeder.ts # Database seeding
โ โ โโโ reset.ts # Database reset utilities
โ โโโ lib/
โ โ โโโ auth.ts # Authentication middleware
โ โโโ types/
โ โโโ app.ts # Type definitions
โโโ tests/ # Test suites
โโโ README.md
Database Schema
Core Tables
- users: User accounts with email/password authentication
- roles: Role definitions (e.g., admin, sales, cashier)
- permissions: Atomic resource-action permissions (e.g., invoice.create)
- policies: Dynamic rule engine with conditions and effects
- invoices: Sample ERP resource for demonstration
Relationships
- user_roles: Many-to-many relationship between users and roles
- role_permissions: Many-to-many relationship between roles and permissions
- user_permissions: Direct user permissions (bypass roles)
Quick Start
Prerequisites
- Node.js โฅ20.x
- PostgreSQL database
- pnpm package manager
Installation
- Clone and install dependencies
git clone https://github.com/mrmeaow/hono-pg-erp-authorization-demo.git erp-auth-demo
cd erp-auth-demo
pnpm install- Environment Setup
cp .env.template .env
# Edit .env with your database credentials- Database Setup
You must make sure that you have created the database for the application e.g.
erp_auth_demodband theerp_auth_demodb_testone too for testing environment.
# Push schema to database
pnpm dkit push # use '--force' if needed
# Seed with initial data
pnpm seed- Start Development Server
pnpm devThe API will be available at http://localhost:3030
Environment Variables
PORT=3030
DEBUG=true
NODE_ENV=dev
DATABASE_URL=postgresql://user:password@localhost:5432/database_nameAPI Endpoints
Authentication
POST /api/auth/register- Register new userPOST /api/auth/login- User loginGET /api/auth/profile- Get user profile (protected)
RBAC Management
GET /api/rbac/roles- List all rolesPOST /api/rbac/roles- Create new roleGET /api/rbac/permissions- List all permissionsPOST /api/rbac/assign-role- Assign role to user
Invoices (Sample Resource)
GET /api/invoices- List user invoicesPOST /api/invoices- Create new invoiceGET /api/invoices/:id- Get specific invoicePUT /api/invoices/:id- Update invoiceDELETE /api/invoices/:id- Delete invoice
Authorization System
RBAC (Role-Based Access Control)
Users are assigned roles, and roles have permissions:
// Example: Assign 'sales' role to user
POST /api/rbac/assign-role
{
"userId": "user-uuid",
"roleNames": ["sales"]
}Direct Permissions
Users can have direct permissions without roles:
// Example: Grant direct invoice.create permission
{
"userId": "user-uuid",
"permissions": ["invoice.create"]
}Policy-Based Access Control (PBAC)
Dynamic policies with conditions:
// Example policy
{
"resource": "invoice",
"actions": ["read", "update"],
"effect": "allow",
"conditions": [
{
"field": "created_by",
"operator": "equals",
"value": "${user.id}"
}
],
"priority": 10
}Available Scripts
# Development
pnpm dev # Start development server
pnpm build # Build for production
pnpm start # Start production server
# Database
pnpm dkit push # Push schema changes
pnpm dkit generate # Generate migrations
pnpm seed # Seed database
pnpm reset # Reset database (recommended for dev/test only)
# Testing
pnpm test # Run test suite
pnpm typecheck # Type checkingTesting
The project includes comprehensive test suites:
# Run all tests
pnpm test
# Test specific areas
NODE_ENV=test pnpm test auth.test.ts
NODE_ENV=test pnpm test rbac.test.ts
NODE_ENV=test pnpm test invoice.test.tsTests automatically use a separate test database and reset between runs.
Database Management
Development Reset
pnpm reset # Truncate and reseed databaseMigrations
pnpm dkit generate # Generate migration from schema changes
pnpm dkit push # Push changes directly (development)
pnpm dkit migrate # Run pending migrations (production)Security Features
- Password Hashing: bcrypt with salt rounds
- JWT Authentication: Secure token-based authentication
- SQL Injection Protection: Drizzle ORM with parameterized queries
- CORS Support: Configurable cross-origin requests
- Input Validation: Type-safe request validation
Permission Examples
Resource-Action Format
invoice.create # Create invoices
invoice.read # Read invoices
invoice.update # Update invoices
invoice.delete # Delete invoices
invoice.approve # Approve invoices
user.manage # Manage users
role.assign # Assign roles
Sample Roles
- admin: Full system access
- sales: Create/read invoices, manage customers
- cashier: Process payments, read invoices
- viewer: Read-only access (Note: not available seeder)
Production Deployment
- Build the application
pnpm build- Set production environment
NODE_ENV=production
DATABASE_URL=your-production-db-url- Run migrations
pnpm dkit migrate- Start the server
pnpm startContributing
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
MIT License - see LICENSE file for details
Support
For questions and support, please create an issue in the repository.
Built with โค๏ธ by gh/mrmeaow using modern TypeScript and battle-tested technologies.