A comprehensive starter template for building SaaS applications with NestJS, MongoDB, and Stripe integration.
-
🔐 Authentication System
- Email/Password authentication
- JWT-based sessions with refresh tokens
- OAuth integration (Google, GitHub, Facebook)
- Role-based access control
-
💰 Subscription Management
- Tiered subscription model
- Usage limitations based on subscription
- Stripe payment integration
- Webhook handling
-
👤 User Management
- Complete user profile system
- Email verification
- Password reset functionality
- Admin user management
-
🛠️ Admin Dashboard
- User administration
- Subscription management
- Analytics and reporting
- System configuration
-
📊 API & Documentation
- Comprehensive API with Swagger documentation
- Rate limiting
- Request validation
- Error handling
- Node.js v16+
- MongoDB
- pnpm (recommended) or npm
- Stripe account for payment processing
- Clone the repository
git clone https://github.com/kolinabir/NEST-Auth-And-SAAS-TEM-MongoDB.git
cd NEST-Auth-And-SAAS-TEM-MongoDB
- Install dependencies
pnpm install
- Configure environment variables
cp .env.example .env
Edit the .env
file with your configuration details.
- Start the development server
pnpm start:dev
- Access the Swagger documentation at http://localhost:4000/api/docs
Variable | Description | Example |
---|---|---|
NODE_ENV |
Environment (development/production) | development |
PORT |
Port to run the server on | 4000 |
MONGODB_URI |
MongoDB connection string | mongodb://localhost:27017/saas-template |
JWT_ACCESS_SECRET |
Secret for JWT access tokens | your_access_token_secret |
JWT_ACCESS_EXPIRATION |
Expiration time for access tokens | 15m |
JWT_REFRESH_SECRET |
Secret for JWT refresh tokens | your_refresh_token_secret |
JWT_REFRESH_EXPIRATION |
Expiration time for refresh tokens | 7d |
FRONTEND_URL |
URL of the frontend application | http://localhost:3000 |
OAUTH_GOOGLE_ENABLED |
Enable Google OAuth | true |
OAUTH_GOOGLE_CLIENT_ID |
Google OAuth client ID | your-google-client-id.apps.googleusercontent.com |
OAUTH_GOOGLE_CLIENT_SECRET |
Google OAuth client secret | your-google-client-secret |
OAUTH_GOOGLE_CALLBACK_URL |
Google OAuth callback URL | http://localhost:4000/auth/google/callback |
OAUTH_FACEBOOK_ENABLED |
Enable Facebook OAuth | false |
OAUTH_FACEBOOK_CLIENT_ID |
Facebook OAuth client ID | your_facebook_client_id |
OAUTH_FACEBOOK_CLIENT_SECRET |
Facebook OAuth client secret | your_facebook_client_secret |
OAUTH_FACEBOOK_CALLBACK_URL |
Facebook OAuth callback URL | http://localhost:4000/auth/facebook/callback |
OAUTH_GITHUB_ENABLED |
Enable GitHub OAuth | false |
OAUTH_GITHUB_CLIENT_ID |
GitHub OAuth client ID | your_github_client_id |
OAUTH_GITHUB_CLIENT_SECRET |
GitHub OAuth client secret | your_github_client_secret |
OAUTH_GITHUB_CALLBACK_URL |
GitHub OAuth callback URL | http://localhost:4000/auth/github/callback |
STRIPE_SECRET_KEY |
Stripe API secret key | sk_test_... |
STRIPE_WEBHOOK_SECRET |
Stripe webhook signing secret | whsec_... |
STRIPE_CURRENCY |
Default currency for payments | usd |
STRIPE_PUBLISHABLE_KEY |
Stripe publishable key for frontend | pk_test_... |
STRIPE_PAYMENT_SUCCESS_URL |
URL to redirect after payment | http://localhost:3000/subscription/success |
STRIPE_PAYMENT_CANCEL_URL |
URL to redirect if payment canceled | http://localhost:3000/subscription/cancel |
src/
├── admin/ # Admin module for user and subscription management
├── auth/ # Authentication with local and OAuth strategies
│ ├── decorators/ # Custom decorators (Public, Roles)
│ ├── dto/ # Data transfer objects for auth operations
│ ├── guards/ # JWT, role-based guards
│ └── strategies/ # Passport strategies (local, JWT, OAuth)
├── common/ # Shared interfaces, utilities and config
├── config/ # Configuration files (app, database, stripe)
├── payments/ # Stripe payment integration
│ ├── dto/ # Payment-related DTOs
│ └── interfaces/ # Stripe webhook event interfaces
├── subscriptions/ # Subscription management
│ ├── dto/ # Subscription DTOs
│ └── schemas/ # Subscription MongoDB schema
├── users/ # User management
│ ├── dto/ # User-related DTOs
│ └── schemas/ # User MongoDB schema
├── app.module.ts # Main application module
└── main.ts # Application entry point
The API documentation is available through Swagger at /api/docs
when the server is running.
Key endpoints include:
-
Authentication:
POST /auth/register
- Register a new userPOST /auth/login
- Login with email and passwordPOST /auth/refresh
- Refresh access tokenGET /auth/google
- Google OAuth loginGET /auth/facebook
- Facebook OAuth loginGET /auth/github
- GitHub OAuth login
-
Users:
GET /users
- List usersGET /users/:id
- Get user profilePATCH /users/:id
- Update userDELETE /users/:id
- Delete user
-
Subscriptions:
GET /subscriptions/tiers
- Get available subscription tiersGET /subscriptions/user/:userId
- Get user subscriptionsPOST /subscriptions/:id/cancel
- Cancel subscription
-
Payments:
POST /payments/checkout
- Create payment checkout sessionPOST /payments/webhook
- Handle Stripe webhooksPOST /payments/portal
- Create customer portal session
-
Admin:
GET /admin/users
- List all users (admin only)PATCH /admin/users/:id
- Update any user (admin only)GET /admin/analytics/users
- Get user analyticsGET /admin/analytics/revenue
- Get revenue analytics
The system includes the following subscription tiers:
-
FREE: Basic features with limited usage
- 1 project
- 100MB storage
- 100 API calls per day
- Community support
-
STARTER: Enhanced features for individuals
- 3 projects
- 1GB storage
- 1,000 API calls per day
- Email support
- $9.99/month or $99.99/year
-
PROFESSIONAL: Full features for professionals
- 10 projects
- 10GB storage
- 10,000 API calls per day
- Priority support
- Advanced analytics
- $29.99/month or $299.99/year
-
ENTERPRISE: Unlimited features for teams
- Unlimited projects
- 100GB storage
- Unlimited API calls
- Dedicated support
- Custom integrations
- SLA guarantees
- $99.99/month or $999.99/year
# Unit tests
pnpm test
# E2E tests
pnpm test:e2e
# Test coverage
pnpm test:cov
pnpm build
For production deployment:
- Build the application
pnpm build
-
Set environment variables for production
-
Start the application
pnpm start:prod
- All passwords are hashed using bcrypt
- JWT tokens are short-lived and require refresh
- Stripe webhooks verify signatures
- OAuth implementations follow security best practices
- Role-based access control for sensitive operations
Contributions are welcome! Please feel free to submit a Pull Request to the GitHub repository.
This project is licensed under the MIT License - see the LICENSE file for details.