Created & Owned by MTalhaZulf
This template is Production Ready
A production-ready NestJS template with Supabase authentication integration and Prisma ORM for database access.
- 🔐 Complete authentication flow with Supabase (signup, signin, signout, account deletion)
- 🔑 Token-based session management with specific token invalidation
- 📊 Prisma ORM integration with PostgreSQL
- 📝 Swagger API documentation
- 🔍 Comprehensive logging with Winston
- ✅ Input validation with class-validator
- 🧪 Testing setup with Jest
- 🛠️ ESLint and Prettier for code quality
- 🔄 Husky and lint-staged for pre-commit hooks
- Node.js (v16 or higher)
- bun or yarn
- PostgreSQL database (or Supabase project)
git clone <repository-url>
cd nestjs_auth_templatebun installCopy the example environment file and update it with your credentials:
cp .env.example .envUpdate the following variables in your .env file:
# Application settings
NODE_ENV=development
PORT=3000
LOG_LEVEL=debug
APP_PREFIX=NestJS-Auth
# Supabase connection
SUPABASE_URL=your_supabase_url
SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_KEY=your_supabase_service_key
# Database connection for Prisma
DATABASE_URL=your_database_url
Execute the SQL triggers to sync Supabase Auth with your database:
npx prisma migrate dev
npx prisma db execute --file ./prisma/manual/supabase_triggers.sql --schema ./prisma/schema.prismaThis creates triggers that automatically:
- Create a user record in your database when a new user signs up via Supabase Auth
- Update user metadata in your database when it's updated in Supabase Auth
npx prisma generate# Development mode
bun run start:dev
# Production mode
bun run build
bun run start:prodPOST /auth/signup- Register a new userPOST /auth/signin- Login with email and passwordPOST /auth/signout- Logout (requires authentication)DELETE /auth/account- Delete user account (requires authentication)
├── prisma/ # Prisma schema and migrations
│ ├── manual/ # Manual SQL scripts
│ └── schema.prisma # Prisma schema definition
├── src/
│ ├── auth/ # Authentication module
│ │ ├── docs/ # API documentation
│ │ ├── dto/ # Data Transfer Objects
│ │ ├── auth.controller.ts
│ │ ├── auth.module.ts
│ │ └── auth.service.ts
│ ├── user/ # User module
│ │ ├── docs/ # API documentation
│ │ ├── dto/ # Data Transfer Objects
│ │ ├── users.controller.ts
│ │ ├── users.module.ts
│ │ └── users.service.ts
│ ├── utils/ # Utility modules
│ │ ├── common/ # Common utilities
│ │ ├── config/ # Configuration
│ │ ├── logger/ # Logging
│ │ ├── prisma/ # Prisma service
│ │ ├── supabase/ # Supabase service
│ │ ├── system/ # System utilities
│ │ └── types/ # TypeScript types
│ ├── app.module.ts # Main application module
│ └── main.ts # Application entry point
└── test/ # Test files
- User submits email, password, and profile information
- The application checks if the email is already in use
- Supabase creates a new user in Auth service
- Database triggers automatically create a user record in the database
- User receives a confirmation email (if enabled in Supabase)
- User submits email and password
- Supabase validates credentials and returns access and refresh tokens
- Tokens are returned to the client for subsequent authenticated requests
- The application receives the current session token from the request headers
- Only the specific token for the current session is invalidated
- This prevents race conditions and allows for signing out from a specific device
- User must be authenticated
- The application deletes the user's data from the database
- Supabase Auth session is terminated
This template implements token-based signout to prevent race conditions and ensure specific token invalidation. When signing out, the token from the request headers is passed to the auth service and then to the Supabase service, which creates a new client with the specific token to invalidate only that session.
Run linting:
bun run lintFormat code:
bun run formatRun tests:
# Unit tests
bun run test
# Test with coverage
bun run test:cov
# E2E tests
bun run test:e2eGenerate architecture dependency graph:
bun run archOnce the application is running, you can access the Swagger documentation at:
http://localhost:3000/api
This project is licensed under the MIT License.