A modern authentication system built with Node.js and Express, featuring multiple authentication strategies including traditional email/password authentication and OAuth integrations (GitHub, Google, Microsoft).
- Email/Password Authentication - Secure registration and login with bcrypt password hashing
- OAuth 2.0 Integration - GitHub, Google, and Microsoft OAuth support
- Session Management - Express sessions with secure cookie handling
- MongoDB Database - Persistent user data storage with Mongoose ODM
- Password Security - Industry-standard bcrypt hashing with 10 salt rounds
- Protected Routes - Session-based route protection middleware
- Docker Support - Full Docker and Docker Compose configuration
- RESTful API - Clean, well-structured API endpoints
- Runtime: Node.js 20+
- Framework: Express 5.x
- Database: MongoDB 7.0+ (NoSQL database for user data persistence)
- ODM: Mongoose (MongoDB object modeling)
- Authentication: Passport.js (GitHub, Google, Microsoft strategies)
- Password Hashing: bcrypt (Industry-standard encryption)
- Session Store: express-session
- Containerization: Docker & Docker Compose (Multi-platform container deployment)
- Secrets Management: Doppler (Environment variable and secrets injection)
- Node.js 20 or higher
- MongoDB 7.0+ (or use Docker for containerized MongoDB)
- Docker & Docker Compose (for containerized deployment)
- npm or yarn
- GitHub OAuth App credentials (for OAuth login)
- Optional: Doppler account for secrets management
- Optional: Google/Microsoft OAuth credentials
git clone <repository-url>
cd login-systemnpm installCreate a .env file in the root directory:
# Server Configuration
PORT=3000
# Database
MONGO_URI=mongodb://localhost:27017/auth_playground
# Session Secret (use a strong random string in production)
SECRET=your-secret-key-here
# GitHub OAuth (Create app at https://github.com/settings/developers)
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
# Optional: Google OAuth
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
# Optional: Microsoft OAuth
MICROSOFT_CLIENT_ID=your-microsoft-client-id
MICROSOFT_CLIENT_SECRET=your-microsoft-client-secretDoppler provides secure secrets management and environment variable injection:
-
Install Doppler CLI:
# macOS brew install doppler # Linux (curl -Ls https://cli.doppler.com/install.sh || wget -qO- https://cli.doppler.com/install.sh) | sh
-
Login and Setup:
doppler login doppler setup
-
Add Secrets:
doppler secrets set PORT=3000 doppler secrets set MONGO_URI='mongodb://localhost:27017/auth_playground' doppler secrets set SECRET='your-secret-key' doppler secrets set GITHUB_CLIENT_ID='your-github-client-id' doppler secrets set GITHUB_CLIENT_SECRET='your-github-client-secret'
-
Run Application with Doppler:
doppler run -- npm start doppler run -- npm run dev
Benefits of Doppler:
- β Centralized secrets management
- β Team collaboration with access controls
- β Automatic secret rotation
- β Environment-specific configurations
- β
No
.envfiles in production - β Audit logs for secret access
- Go to GitHub Developer Settings
- Create a new OAuth App
- Set Authorization callback URL to:
http://localhost:3000/api/auth/github/callback - Copy Client ID and Client Secret to
.env
- Go to Google Cloud Console
- Create a new project and enable Google+ API
- Create OAuth 2.0 credentials
- Add
http://localhost:3000/api/auth/google/callbackto authorized redirect URIs
- Go to Azure Portal
- Register a new application
- Add
http://localhost:3000/api/auth/microsoft/callbackto redirect URIs
# Start MongoDB (if running locally)
mongod
# Run in development mode with auto-reload
npm run dev
# Or run in production mode
npm start# Build and run with Docker Compose
docker-compose up -d
# View logs
docker-compose logs -f backend
# Stop containers
docker-compose downThe API will be available at http://localhost:3000
POST /api/auth/signup
Content-Type: application/json
{
"email": "user@example.com",
"password": "your-password"
}Response:
{
"message": "User created successfully"
}POST /api/auth/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "your-password"
}Response:
{
"message": "Logged in"
}POST /api/auth/logoutResponse:
{
"message": "Logged out"
}Visit in browser:
GET /api/auth/github
Redirects to GitHub for authentication, then back to / on success.
GET /api/auth/google
GET /api/auth/microsoft
GET /api/protectedResponse (Authenticated):
{
"message": "Accessing protected"
}Response (Unauthenticated):
{
"message": "Unauthorized"
}GET /healthResponse:
{
"status": "OK"
}login-system/
βββ src/
β βββ config/
β β βββ db.js # MongoDB connection configuration
β β βββ passport.js # Passport strategies configuration
β βββ controllers/
β β βββ auth.controller.js # Authentication logic
β βββ middleware/
β β βββ auth.middleware.js # Session validation middleware
β βββ models/
β β βββ User.js # User schema and methods
β βββ routes/
β β βββ auth.routes.js # Authentication routes
β βββ server.js # Express app entry point
βββ .env # Environment variables (create this)
βββ .gitignore
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Docker container definition
βββ package.json
βββ README.md
- Password Hashing: Passwords are hashed using bcrypt with 10 salt rounds
- Session Security: Secure session management with httpOnly cookies
- Environment Variables: Sensitive data stored in environment variables
- OAuth 2.0: Industry-standard OAuth implementation
- No Password Storage for OAuth: Users authenticated via OAuth don't have passwords stored
The application is fully containerized and can be deployed using Docker:
# Build the image
docker build -t login-system .
# Run with Docker Compose
docker-compose up -dServices:
- backend: Node.js application (port 5000 β 3000)
- mongo: MongoDB database (port 27017)
Volumes:
DB_data: Persistent MongoDB data storage
# Sign up
curl -X POST http://localhost:3000/api/auth/signup \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123"}'
# Login
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"password123"}' \
-c cookies.txt
# Access protected route (using saved cookies)
curl http://localhost:3000/api/protected -b cookies.txt
# Logout
curl -X POST http://localhost:3000/api/auth/logout -b cookies.txt- Import the API endpoints
- Enable cookie handling in Postman settings
- Test the authentication flow
| Variable | Description | Required | Default |
|---|---|---|---|
PORT |
Server port | No | 3000 |
MONGO_URI |
MongoDB connection string | Yes | - |
SECRET |
Session secret key | Yes | - |
GITHUB_CLIENT_ID |
GitHub OAuth client ID | For OAuth | - |
GITHUB_CLIENT_SECRET |
GitHub OAuth secret | For OAuth | - |
GOOGLE_CLIENT_ID |
Google OAuth client ID | Optional | - |
GOOGLE_CLIENT_SECRET |
Google OAuth secret | Optional | - |
MICROSOFT_CLIENT_ID |
Microsoft OAuth client ID | Optional | - |
MICROSOFT_CLIENT_SECRET |
Microsoft OAuth secret | Optional | - |
Sessions are configured with:
- Cookie name:
connect.sid - saveUninitialized:
false - resave:
false
{
email: String (required, unique),
password: String (required for email/password auth),
githubId: String (for GitHub OAuth users),
role: String (default: "user"),
createdAt: Date,
updatedAt: Date
}- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add 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.
Lalith
- Passport.js for authentication strategies
- Express.js team for the excellent framework
- MongoDB team for the database
- bcrypt for password hashing
For issues, questions, or contributions, please open an issue on GitHub.
Note: This is a playground project for learning authentication patterns. For production use, consider additional security measures such as:
- Rate limiting
- CSRF protection
- Account lockout policies
- Email verification
- Two-factor authentication
- Security headers (helmet.js)
- Input validation and sanitization