Skip to content

Repository files navigation

ModSecurity Backend API

A Node.js backend API with Express, Swagger documentation, and Prisma ORM for Supabase PostgreSQL database.

Features

  • 🚀 Express.js server with TypeScript
  • 📚 Auto-generated Swagger API documentation at /docs
  • 🗄️ Prisma ORM with PostgreSQL (Supabase)
  • 🔐 Supabase Authentication integration
  • 🔒 Type-safe database operations
  • 🎯 RESTful API endpoints

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Supabase account and database

Setup

  1. Install dependencies:

    npm install
  2. Set up environment variables:

    • Create a .env file in the root directory

    • Add the following variables:

      # Database
      # Connect to Supabase via connection pooling (for queries)
      DATABASE_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-1-eu-west-1.pooler.supabase.com:6543/postgres?pgbouncer=true"
      
      # Direct connection to the database (for migrations)
      DIRECT_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-1-eu-west-1.pooler.supabase.com:5432/postgres"
      
      # Supabase Auth
      SUPABASE_URL="https://your-project.supabase.co"
      SUPABASE_ANON_KEY="your-anon-key"
      SUPABASE_SERVICE_ROLE_KEY="your-service-role-key"
      
      # Server
      PORT=3001
      NODE_ENV=development
      
      # Frontend
      FRONTEND_URL="http://localhost:3000"
      
      # Seed Admin User (Optional - for customizing the default admin)
      ADMIN_EMAIL="admin@example.com"
      ADMIN_PASSWORD="Admin123!"
      ADMIN_FULL_NAME="Super Admin"
      ADMIN_ID="00000000-0000-0000-0000-000000000001"
  3. Generate Prisma Client:

    npm run prisma:generate
  4. Run database migrations:

    npm run prisma:migrate

    Or push schema directly (for development):

    npm run prisma:push
  5. Seed the database with a super admin user:

    npm run seed

    This will create a default super admin user:

    • Email: admin@example.com (or set ADMIN_EMAIL in .env)
    • Password: Admin123! (or set ADMIN_PASSWORD in .env)
    • Full Name: Super Admin (or set ADMIN_FULL_NAME in .env)
    • Role: super_admin

    You can customize these by adding to your .env:

    ADMIN_EMAIL="your-admin@example.com"
    ADMIN_PASSWORD="YourSecurePassword123!"
    ADMIN_FULL_NAME="Your Name"
    ADMIN_ID="00000000-0000-0000-0000-000000000001"  # Optional: custom UUID
  6. Start the development server:

    npm run dev

API Documentation

Once the server is running, visit:

Available Scripts

  • npm run dev - Start development server with hot reload
  • npm run build - Build for production
  • npm start - Start production server
  • npm run prisma:generate - Generate Prisma Client
  • npm run prisma:migrate - Run database migrations
  • npm run prisma:push - Push schema to database (dev only)
  • npm run prisma:studio - Open Prisma Studio (database GUI)
  • npm run seed - Seed database with super admin user

API Endpoints

Authentication

  • POST /api/auth/login - Login with email and password (uses Supabase Auth)
  • POST /api/auth/logout - Logout user
  • GET /api/auth/me - Get current authenticated user

Users

  • GET /api/users - Get all users
  • GET /api/users/:id - Get user by ID
  • POST /api/users - Create a new user
  • PUT /api/users/:id - Update user
  • DELETE /api/users/:id - Delete user

Organizations

  • GET /api/organizations - Get all organizations
  • GET /api/organizations/my - Get organizations where current user is a member
  • GET /api/organizations/:id - Get organization by ID
  • POST /api/organizations - Create a new organization (requires auth)
  • PUT /api/organizations/:id - Update organization (requires auth)
  • DELETE /api/organizations/:id - Delete organization (requires auth)

Database Schema

The Prisma schema includes:

  • User - User accounts with:
    • id - UUID (primary key)
    • email - Unique email address
    • fullName - Optional full name
    • role - Optional role (super_admin or null)
    • createdAt - Timestamp
    • updatedAt - Timestamp

Project Structure

modsecurity-back-end/
├── src/
│   ├── lib/
│   │   ├── prisma.ts
│   │   └── supabase.ts
│   ├── routes/
│   │   ├── auth.routes.ts
│   │   └── user.routes.ts
│   └── server.ts
├── prisma/
│   └── schema.prisma
├── .env
├── package.json
├── tsconfig.json
└── README.md

Development

The server runs on port 3001 by default. You can change this in your .env file.

For Supabase configuration:

  • DATABASE_URL: Connection pooling URL (port 6543 with ?pgbouncer=true) - used for queries
    • Find in Supabase project settings under "Database" → "Connection string" → "Connection pooling" → "URI"
  • DIRECT_URL: Direct connection URL (port 5432) - used for migrations
    • Find in Supabase project settings under "Database" → "Connection string" → "URI" (direct connection)
  • SUPABASE_URL: Found in your Supabase project settings under "API" → "Project URL"
  • SUPABASE_ANON_KEY: Found in your Supabase project settings under "API" → "Project API keys" → "anon public"
  • SUPABASE_SERVICE_ROLE_KEY: Found in your Supabase project settings under "API" → "Project API keys" → "service_role" (secret)
    • ⚠️ Important: Keep this key secure! It has admin privileges and bypasses Row Level Security (RLS)
    • Used for sending invitation emails and creating users in Supabase Auth

Note: Prisma requires both DATABASE_URL (pooled) and DIRECT_URL (direct) for Supabase. The pooled connection is used for queries, while the direct connection is required for migrations.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages