A robust, production-ready authentication API built with Node.js, Express, and MongoDB
- BCrypt Password Hashing - Your users' passwords are encrypted with industry-standard bcrypt (10 salt rounds)
- JWT Token Authentication - Secure, stateless authentication with 1-hour token expiration
- Token Storage in Database - Full control over active sessions with token revocation on logout
- Middleware Protection - Routes are guarded with token verification middleware
- Async/Await Architecture - Non-blocking operations for maximum throughput
- MongoDB Integration - NoSQL database optimized for high-performance user management
- Token Auto-Expiry - Automatic cleanup of expired tokens (1 hour TTL)
- Efficient Password Comparison - Promise-based bcrypt operations
- β Create new users with automatic password hashing
- β Read all users or get current session user
- β Update user information dynamically
- β Delete users securely
- Clean MVC Architecture - Separated controllers, models, services, and routes
- RESTful API Design - Intuitive endpoint structure
- Comprehensive Error Handling - Proper status codes and error messages
- Easy Integration - JSON-based request/response format
| Feature | Description | Status |
|---|---|---|
| π User Registration | Create account with encrypted password | β Ready |
| π Login System | JWT-based authentication | β Ready |
| πͺ Logout | Token revocation on logout | β Ready |
| π€ User Profile | Get current authenticated user info | β Ready |
| π User Management | Full CRUD operations | β Ready |
| π Protected Routes | Middleware-based route protection | β Ready |
| β° Token Expiration | Auto-expiring sessions (1 hour) | β Ready |
User-Authentication-System/
β
βββ π controllers/
β βββ authController.js # Login & logout logic
β βββ userController.js # CRUD operations
β βββ sessionController.js # Current user info
β
βββ π models/
β βββ User.js # User schema with pre-save hashing
β βββ AuthToken.js # Token storage schema
β
βββ π services/
β βββ authService.js # JWT generation
β βββ bcryptService.js # Password hashing/comparison
β
βββ π middleware/
β βββ verifyToken.js # JWT verification middleware
β
βββ π routes/
β βββ authRoutes.js # /api/auth endpoints
β βββ userRoutes.js # /api/users endpoints
β βββ sessionRoutes.js # /api/session endpoints
β
βββ π db/
β βββ db.js # MongoDB connection
β
βββ server.js # Entry point (Port 3010)
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
# Clone the repository
git clone https://github.com/majoymajo/User-Authentication-System.git
# Navigate to project directory
cd User-Authentication-System
# Install dependencies
npm install
# Start the server
npm startThe server will run on http://localhost:3010 π
POST /api/auth/login
Content-Type: application/json
{
"email": "user@example.com",
"contraseΓ±a": "yourPassword"
}Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}POST /api/auth/logout
Authorization: Bearer <your_token>GET /api/usersPOST /api/users
Content-Type: application/json
{
"nombre": "John Doe",
"edad": 25,
"email": "john@example.com",
"contraseΓ±a": "securePassword123"
}PUT /api/users/:id
Content-Type: application/json
{
"nombre": "Jane Doe",
"edad": 26
}DELETE /api/users/:idGET /api/session/current
Authorization: Bearer <your_token>- Passwords are never stored in plain text
- BCrypt hashing happens automatically via Mongoose pre-save hooks
- 10 salt rounds ensure strong encryption
- JWT tokens are signed with a secure secret key
- Tokens expire after 1 hour
- Stored in database for logout functionality
- Middleware verification protects sensitive routes
- Email field has
unique: trueconstraint - Prevents duplicate accounts
This project demonstrates:
β
Modern JavaScript - ES6+ syntax with promises and async operations
β
RESTful API Design - Proper HTTP methods and status codes
β
Database Modeling - Mongoose schemas with validation
β
Authentication Flow - Complete login/logout cycle
β
Middleware Pattern - Reusable token verification
β
Service Layer - Separation of business logic
β
Error Handling - Comprehensive try-catch and promise rejection handling
| Technology | Purpose |
|---|---|
| Express.js | Web framework for building REST APIs |
| MongoDB | NoSQL database for user storage |
| Mongoose | ODM for MongoDB |
| bcrypt | Password hashing library |
| jsonwebtoken | JWT creation and verification |
Contributions are welcome! Feel free to:
- π Report bugs
- π‘ Suggest new features
- π§ Submit pull requests
This project is open source and available under the MIT License.
majoymajo
GitHub: @majoymajo
If you find this project useful, please give it a β on GitHub!
Built with β€οΈ and JavaScript
Secure β’ Scalable β’ Simple