A robust Express.js authentication API with advanced security features, built with TypeScript, Drizzle ORM, and comprehensive authentication strategies.
-
π Multiple Authentication Methods
- Local authentication (email/password)
- Google OAuth 2.0 integration
- Passport.js integration
-
π‘οΈ Security Features
- CSRF protection
- Rate limiting
- Helmet security headers
- Session management
- Password hashing with bcrypt
- Input validation with Zod
-
π§ Email Services
- Email verification
- Password reset
- Template-based emails with Handlebars
- Nodemailer integration
-
ποΈ Database Support
- PostgreSQL with Drizzle ORM
- MongoDB with Mongoose
- Database migrations and seeding
-
π§ Developer Experience
- TypeScript support
- Hot reload with Nodemon
- ESLint and Prettier configuration
- Comprehensive error handling
- Structured logging
- Backend: Express.js, TypeScript
- Database: PostgreSQL (Drizzle ORM), MongoDB (Mongoose)
- Authentication: Passport.js, bcrypt
- Validation: Zod
- Email: Nodemailer, Handlebars
- Security: Helmet, CORS, Rate Limiting, CSRF
- File Upload: Multer, Cloudinary
- Build Tools: tsup, ESLint, Prettier
- Node.js >= 20.0.0
- PostgreSQL database
- MongoDB database (optional)
- PNPM package manager
-
Clone the repository
git clone https://github.com/tajbinkhan/express-authentication-v2.git cd express-authentication-v2
-
Install dependencies
pnpm install
-
Environment Setup
cp .env.example .env
Fill in your environment variables:
# Database DATABASE_URL=postgresql://username:password@localhost:5432/dbname MONGODB_URI=mongodb://localhost:27017/your-db-name # Session SESSION_SECRET=your-super-secret-session-key # Google OAuth GOOGLE_CLIENT_ID=your-google-client-id GOOGLE_CLIENT_SECRET=your-google-client-secret # Email SMTP_HOST=smtp.example.com SMTP_PORT=587 SMTP_USER=your-email@example.com SMTP_PASS=your-email-password # Cloudinary (for file uploads) CLOUDINARY_CLOUD_NAME=your-cloud-name CLOUDINARY_API_KEY=your-api-key CLOUDINARY_API_SECRET=your-api-secret
-
Database Setup
# Generate and run migrations pnpm db:generate pnpm db:migrate # Seed the database pnpm db:seed
# Start development server with hot reload
pnpm dev
# Build for production
pnpm build
# Start production server
pnpm start
# Run linting
pnpm lint
# Format code
pnpm format
# Type checking
pnpm type-check
# Database studio (GUI)
pnpm db:studio
# Generate migrations
pnpm db:generate
# Run migrations
pnpm db:migrate
# Push schema changes
pnpm db:push
# Clear database
pnpm db:clear
# Seed database
pnpm db:seed
# Reset database (clear + migrate + seed)
pnpm db:reset
POST /auth/register
- User registrationPOST /auth/login
- User loginPOST /auth/logout
- User logoutGET /auth/google
- Google OAuth loginGET /auth/google/callback
- Google OAuth callback
GET /users/profile
- Get user profilePUT /users/profile
- Update user profilePOST /users/change-password
- Change password
POST /email/verify
- Send verification emailPOST /email/forgot-password
- Send password reset emailPOST /email/reset-password
- Reset password
src/
βββ app/ # Feature modules
β βββ authentication/ # Auth controllers, services, routes
β βββ email/ # Email functionality
β βββ emailTemplate/ # Email template services
β βββ user/ # User management
βββ core/ # Core utilities and constants
βββ databases/ # Database configurations and helpers
βββ mailer/ # Email configuration and services
βββ middlewares/ # Custom middleware
βββ models/ # Database models
βββ passport/ # Passport strategies
βββ routes/ # Route configurations
βββ seed/ # Database seeders
βββ service/ # Business logic services
βββ settings/ # App configuration
βββ utils/ # Utility functions
βββ validators/ # Input validation schemas
- CSRF Protection: Double submit cookie pattern
- Rate Limiting: Configurable rate limits per endpoint
- Input Validation: Zod schema validation
- SQL Injection Prevention: Parameterized queries with Drizzle ORM
- XSS Protection: Helmet security headers
- Session Security: Secure session configuration
- Password Security: bcrypt hashing with salt rounds
- 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
- Follow TypeScript best practices
- Write tests for new features
- Follow the existing code style (ESLint/Prettier)
- Update documentation as needed
- Ensure all tests pass before submitting PR
# Run tests (when implemented)
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverage
- Install Vercel CLI:
npm i -g vercel
- Run:
vercel
- Follow the deployment prompts
# Build Docker image
docker build -t express-auth-v2 .
# Run container
docker run -p 3000:3000 express-auth-v2
Make sure to set all required environment variables in your production environment:
NODE_ENV=production
DATABASE_URL
SESSION_SECRET
GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET
- Email configuration variables
- Cloudinary configuration variables
-
Database Connection Issues
- Verify your
DATABASE_URL
is correct - Ensure PostgreSQL is running
- Check firewall settings
- Verify your
-
OAuth Issues
- Verify Google OAuth credentials
- Check redirect URIs in Google Console
- Ensure correct domain configuration
-
Email Issues
- Verify SMTP configuration
- Check email provider settings
- Ensure firewall allows SMTP connections
- Database connection pooling
- Session store optimization
- Rate limiting to prevent abuse
- Efficient query patterns with Drizzle ORM
- Optimized middleware ordering
- Structured logging with custom logger
- Request/response logging
- Error tracking and reporting
- Performance monitoring capabilities
This project is licensed under the MIT License - see the LICENSE file for details.
- Express.js - Fast, unopinionated, minimalist web framework
- Passport.js - Simple, unobtrusive authentication
- Drizzle ORM - TypeScript ORM for SQL databases
- Zod - TypeScript-first schema validation
If you encounter any issues or have questions, please:
- Check the documentation
- Search existing issues
- Create a new issue if needed
Made with β€οΈ by Tajbin Khan