A secure, multi-tenant notes application with JWT authentication, role-based access control, and subscription feature gating.
This application uses a shared schema with tenant ID column approach. All data is stored in shared data structures with tenant isolation enforced through:
- Tenant ID filtering on all queries
- JWT token validation containing tenant context
- Strict access controls preventing cross-tenant data access
- Multi-tenant architecture with strict data isolation
- JWT-based authentication with role-based access control
- Subscription feature gating (Free: 3 notes limit, Pro: unlimited)
- CRUD operations for notes with tenant isolation
- Admin upgrade functionality for subscription management
- Responsive UI built with React and Tailwind CSS
All accounts use password: password
admin@acme.test(Admin, Acme tenant)user@acme.test(Member, Acme tenant)admin@globex.test(Admin, Globex tenant)user@globex.test(Member, Globex tenant)
GET /api/health- Health checkPOST /api/auth/login- User loginGET /api/notes- List tenant notesPOST /api/notes- Create note (with limits)GET /api/notes/:id- Get specific notePUT /api/notes/:id- Update noteDELETE /api/notes/:id- Delete notePOST /api/tenants/:slug/upgrade- Upgrade tenant (Admin only)
- MongoDB installed locally or MongoDB Atlas account
- Node.js and npm installed
-
Install MongoDB locally (if not already installed):
- Windows: Download from MongoDB Community Server
- macOS:
brew install mongodb-community - Linux: Follow MongoDB installation guide
-
Start MongoDB service:
# Windows (if installed as service) net start MongoDB # macOS/Linux brew services start mongodb-community # or sudo systemctl start mongod
-
Configure environment variables: The
.env.localfile is already configured with:MONGODB_URI=mongodb://localhost:27017/multitenantnotesapp JWT_SECRET=your_jwt_secret_key
-
Create MongoDB Atlas account at mongodb.com/cloud/atlas
-
Create a cluster and get your connection string
-
Update
.env.local:MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/multitenantnotesapp
Run the setup script to create sample data:
npm run setup-dbThis will:
- Connect to MongoDB
- Create sample tenants (Acme, Globex)
- Create sample users with test accounts
- Set up the database schema
- Tenants: Company/organization data with subscription plans
- Users: User accounts with roles (admin/member) and tenant association
- Notes: User notes with tenant isolation
-
Install dependencies:
npm install
-
Start development server:
npm run dev
-
Access the application:
- Open http://localhost:3000
- Use test accounts from the setup
Create a .env.local file in the root directory with the following variables:
# Database
MONGODB_URI=mongodb://localhost:27017/multitenantnotesapp
# or for MongoDB Atlas:
# MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/multitenantnotesapp
# JWT Secret (use a strong, random string in production)
JWT_SECRET=your_super_secret_jwt_key_change_in_production
# Next.js
NEXTAUTH_URL=http://localhost:3000-
Install Vercel CLI:
npm i -g vercel
-
Deploy:
vercel
-
Set environment variables in Vercel dashboard:
MONGODB_URIJWT_SECRET
-
Create Dockerfile:
FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . RUN npm run build EXPOSE 3000 CMD ["npm", "start"]
-
Build and run:
docker build -t multitenant-notes-app . docker run -p 3000:3000 --env-file .env.local multitenant-notes-app
-
Build the application:
npm run build
-
Start production server:
npm start
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
# Run E2E tests
npm run test:e2e- API Tests:
tests/api/- Tests for all API endpoints - E2E Tests:
tests/e2e/- End-to-end frontend tests - Unit Tests: Component and utility function tests
The test suite covers:
- Authentication and authorization
- CRUD operations with tenant isolation
- Role-based access control
- Subscription limits and upgrades
- Frontend accessibility and responsiveness
multitenantnotesapp/
├── src/
│ ├── app/ # Next.js App Router
│ │ ├── api/ # API routes
│ │ │ ├── auth/ # Authentication endpoints
│ │ │ ├── notes/ # Notes CRUD operations
│ │ │ ├── tenants/ # Tenant management
│ │ │ └── users/ # User management
│ │ ├── dashboard/ # Main dashboard page
│ │ ├── login/ # Login page
│ │ ├── signup/ # Registration page
│ │ └── globals.css # Global styles
│ ├── components/ # React components
│ │ ├── CreateNote.tsx # Note creation form
│ │ ├── NotesList.tsx # Notes display component
│ │ ├── UserManagement.tsx # User management interface
│ │ └── SubscriptionStatus.tsx # Subscription info
│ ├── lib/ # Utility libraries
│ │ ├── auth.ts # JWT authentication
│ │ ├── db.ts # Database operations
│ │ ├── mongodb.ts # MongoDB connection
│ │ ├── rbac.ts # Role-based access control
│ │ └── types.ts # TypeScript interfaces
│ ├── models/ # MongoDB models
│ │ ├── User.ts # User schema
│ │ ├── Tenant.ts # Tenant schema
│ │ └── Note.ts # Note schema
│ └── types/ # TypeScript type definitions
├── tests/ # Test files
│ ├── api/ # API tests
│ ├── e2e/ # End-to-end tests
│ └── setup.js # Test setup
├── scripts/ # Utility scripts
│ └── setup-db.js # Database initialization
├── public/ # Static assets
├── package.json # Dependencies and scripts
├── next.config.ts # Next.js configuration
├── tailwind.config.js # Tailwind CSS configuration
└── tsconfig.json # TypeScript configuration
- Tenant ID Filtering: All database queries include tenant ID filtering
- JWT Token Validation: Every request validates tenant context from JWT
- Strict Access Controls: Prevents cross-tenant data access at API level
- JWT-based Authentication: Secure token-based authentication
- Role-based Access Control: Admin and member roles with different permissions
- Password Hashing: bcrypt for secure password storage
- Token Expiration: 7-day token expiration for security
- Input Validation: All API endpoints validate input data
- Error Handling: Secure error messages without sensitive information
- Rate Limiting: Built-in Next.js rate limiting
- CORS Protection: Configured for production environments
- Next.js App Router: Latest Next.js routing for optimal performance
- Tailwind CSS: Utility-first CSS for minimal bundle size
- Component Optimization: React hooks and memoization
- Responsive Design: Mobile-first responsive layout
- MongoDB Indexing: Optimized database queries
- Connection Pooling: Efficient database connections
- JWT Caching: Token validation optimization
- API Response Caching: Strategic caching for better performance
- Console Logging: Detailed logs for development debugging
- Error Tracking: Comprehensive error handling and logging
- Performance Monitoring: Built-in Next.js performance metrics
- Health Check Endpoint:
/api/healthfor monitoring - Error Boundaries: React error boundaries for graceful failures
- Logging: Structured logging for production monitoring
-
Fork the repository
-
Clone your fork:
git clone https://github.com/yourusername/multitenantnotesapp.git cd multitenantnotesapp -
Install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env.local # Edit .env.local with your configuration -
Initialize database:
npm run setup-db
-
Start development server:
npm run dev
- TypeScript: Strict TypeScript configuration
- ESLint: Code linting with Next.js recommended rules
- Prettier: Code formatting (if configured)
- Conventional Commits: Use conventional commit messages
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes and test thoroughly
- Run tests:
npm test - Run build:
npm run build - Commit changes:
git commit -m 'feat: add amazing feature' - Push to branch:
git push origin feature/amazing-feature - Open a Pull Request
# Check MongoDB service status
# Windows
net start MongoDB
# macOS/Linux
brew services start mongodb-community
# or
sudo systemctl start mongod# Clear Next.js cache
rm -rf .next
# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install
# Rebuild
npm run build- Verify JWT_SECRET is set in environment variables
- Check token expiration (7 days default)
- Ensure user has correct tenant association
- Issues: Create an issue on GitHub
- Documentation: Check this README and inline code comments
- Community: Join discussions in GitHub Discussions
This project is licensed under the MIT License - see the LICENSE file for details.
- Next.js - React framework for production
- MongoDB - NoSQL database
- Tailwind CSS - Utility-first CSS framework
- JWT - JSON Web Token authentication
- bcrypt - Password hashing library
Built with ❤️ using Next.js, MongoDB, and Tailwind CSS