A complete authentication system built with modern technologies including user registration, login, email verification, password reset, and protected routes.
- Node.js + TypeScript + Express.js
- MongoDB database with Mongoose ODM
- JWT (access & refresh tokens) for authentication
- bcrypt for password hashing
- Email verification and password reset via email
- Security: helmet, cors, rate limiting, input sanitization
- Testing: Jest with supertest
- Validation: Zod schemas
- React 18 + TypeScript + Vite
- Tailwind CSS for styling
- React Router for navigation
- Redux Toolkit for state management
- Axios with interceptors for API calls
- Responsive design with loading states
- Docker + Docker Compose for containerization
- ESLint + Prettier for code quality
- Environment configuration with dotenv
auth-system/
βββ backend/ # Node.js + TypeScript backend
β βββ src/
β β βββ controllers/ # Route controllers
β β βββ middleware/ # Authentication middleware
β β βββ routes/ # API routes
β β βββ services/ # Business logic
β β βββ utils/ # Utility functions
β βββ models/ # MongoDB models & schemas
β βββ tests/ # Jest tests
β βββ Dockerfile # Backend container
βββ frontend/ # React + TypeScript frontend
β βββ src/
β β βββ components/ # Reusable components
β β βββ pages/ # Page components
β β βββ store/ # Redux store & slices
β β βββ types/ # TypeScript types
β β βββ utils/ # API utilities
β βββ Dockerfile # Frontend container
βββ docker-compose.yml # Full stack orchestration
-
Clone and setup:
git clone <repo-url> cd auth-system
-
Configure environment (copy and edit):
cp backend/.env.example backend/.env cp frontend/.env.example frontend/.env
-
Start all services:
docker-compose up -d
-
Access the application:
- Frontend: http://localhost:3000
- Backend API: http://localhost:4000
- Database: localhost:27017
- Node.js 18+
- MongoDB (local installation or MongoDB Atlas)
- npm or yarn
-
Install dependencies:
cd backend npm install
-
Configure environment:
cp .env.example .env # Edit .env with your database credentials and email settings
-
Start development server:
npm run dev
-
Install dependencies:
cd frontend npm install
-
Configure environment:
cp .env.example .env # Edit .env if needed (API URL)
-
Start development server:
npm run dev
# Server Configuration
PORT=4000
MONGODB_URI=mongodb://localhost:27017/authdb
# JWT Secrets (MUST CHANGE - Generate random strings)
JWT_SECRET=your_super_secret_jwt_key_CHANGE_THIS
JWT_REFRESH_SECRET=your_super_secret_refresh_key_CHANGE_THIS
# Email Configuration (Replace with your email settings)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USER=your_email@gmail.com # Your actual Gmail
EMAIL_PASS=your_app_password # Your Gmail App Password
EMAIL_FROM=no-reply@yourdomain.com # Your sender email
# Frontend URL
CLIENT_URL=http://localhost:3000
- Generate strong JWT secrets using:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
- For Gmail: Enable 2FA and create an App Password
- Never commit your actual
.env
file to Git
VITE_API_URL=http://localhost:4000
POST /auth/register
- User registrationPOST /auth/login
- User loginPOST /auth/logout
- User logoutPOST /auth/refresh
- Refresh access tokenGET /auth/verify-email/:token
- Email verificationPOST /auth/request-password-reset
- Request password resetPOST /auth/reset-password/:token
- Reset password
GET /profile
- Get user profile (requires authentication)
cd backend
npm test
npm test auth.test.ts
# Build and start production containers
docker-compose up -d --build
- Set production environment variables
- Build frontend:
npm run build
- Build backend:
npm run build
- Start services with process manager (PM2, etc.)
- JWT Authentication with access & refresh tokens
- Password hashing with bcrypt
- Email verification before account activation
- Password reset with secure tokens
- Rate limiting to prevent abuse
- Input sanitization against XSS
- CORS configuration
- Helmet for security headers
- Cookie security with httpOnly flags
- β User registration with email verification
- β Secure login/logout
- β JWT-based authentication
- β Password reset via email
- β Protected routes and middleware
- β Responsive design with Tailwind CSS
- β Form validation and error handling
- β Loading states and user feedback
- β Automatic token refresh
- β Route protection and redirects
- β TypeScript for type safety
- β ESLint + Prettier for code quality
- β Hot reload in development
- β Comprehensive testing
- β Docker containerization
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Ensure MongoDB is running
- Verify MONGODB_URI in .env
- Check database credentials
- Configure SMTP settings in .env
- For Gmail, use App Passwords
- Check firewall/network settings
- Clear node_modules and reinstall
- Check Node.js version compatibility
- Verify environment variables
- Port already in use: Change PORT in .env
- JWT errors: Verify JWT_SECRET is set
- CORS errors: Check CLIENT_URL configuration