Skip to content

majoymajo/User-Authentication-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” User Authentication System

A robust, production-ready authentication API built with Node.js, Express, and MongoDB

JavaScript Node.js Express.js MongoDB


✨ Why Choose This System?

πŸ›‘οΈ Enterprise-Grade Security

  • 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

⚑ Performance & Scalability

  • 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

🎯 Complete CRUD Operations

  • βœ… Create new users with automatic password hashing
  • βœ… Read all users or get current session user
  • βœ… Update user information dynamically
  • βœ… Delete users securely

πŸš€ Developer-Friendly

  • 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

πŸ“‹ Features at a Glance

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

πŸ—οΈ Project Structure

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)

πŸš€ Quick Start

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or cloud instance)

Installation

# 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 start

The server will run on http://localhost:3010 πŸŽ‰


πŸ“‘ API Endpoints

Authentication Routes (/api/auth)

πŸ”“ Login

POST /api/auth/login
Content-Type: application/json

{
  "email": "user@example.com",
  "contraseΓ±a": "yourPassword"
}

Response:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

πŸšͺ Logout

POST /api/auth/logout
Authorization: Bearer <your_token>

User Management Routes (/api/users)

πŸ‘₯ Get All Users

GET /api/users

βž• Create User

POST /api/users
Content-Type: application/json

{
  "nombre": "John Doe",
  "edad": 25,
  "email": "john@example.com",
  "contraseΓ±a": "securePassword123"
}

✏️ Update User

PUT /api/users/:id
Content-Type: application/json

{
  "nombre": "Jane Doe",
  "edad": 26
}

❌ Delete User

DELETE /api/users/:id

Session Routes (/api/session)

πŸ‘€ Get Current User

GET /api/session/current
Authorization: Bearer <your_token>

πŸ”’ Security Highlights

Password Protection

  • Passwords are never stored in plain text
  • BCrypt hashing happens automatically via Mongoose pre-save hooks
  • 10 salt rounds ensure strong encryption

Token Management

  • 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 Uniqueness

  • Email field has unique: true constraint
  • Prevents duplicate accounts

πŸŽ“ Learning Highlights

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


πŸ› οΈ Tech Stack

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

🀝 Contributing

Contributions are welcome! Feel free to:

  • πŸ› Report bugs
  • πŸ’‘ Suggest new features
  • πŸ”§ Submit pull requests

πŸ“„ License

This project is open source and available under the MIT License.


πŸ‘¨β€πŸ’» Author

majoymajo
GitHub: @majoymajo


🌟 Show Your Support

If you find this project useful, please give it a ⭐ on GitHub!


Built with ❀️ and JavaScript

Secure β€’ Scalable β€’ Simple

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •