A secure REST API implementation for user authentication using JSON Web Tokens (JWT).
- 📝 User registration (sign-up)
- 🔑 User authentication (sign-in)
- 🚪 User sign-out
- 🔒 Secure password hashing using bcrypt
- 🎟️ JWT-based authentication
- 🗄️ MongoDB integration
⚠️ Error handling middleware- ✅ Input validation
- 📦 Node.js (v14 or higher)
- 🗃️ MongoDB database
- 📥 npm or yarn package manager
- Clone the repository:
git clone <repository-url>
cd user-authenticator- Install dependencies:
npm install- Create a
.envfile in the root directory with the following variables:
PORT=8000
DB_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=1dnpm run devnpm start| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/auth/sign-up |
Register a new user |
| POST | /api/v1/auth/sign-in |
Authenticate user and get token |
| POST | /api/v1/auth/sign-out |
Sign out user |
{
"username": "username",
"password": "Password123!"
}{
"username": "username",
"password": "Password123!"
}- 🔒 Password validation (minimum 8 characters, uppercase, lowercase, special character)
- 👤 Username validation (8-50 characters)
- 🍪 HTTP-only cookies
- 🔑 Secure password hashing
- 💾 MongoDB transaction support
- 🧹 Input sanitization
- 🌐 CORS enabled
The API includes comprehensive error handling for:
- ❌ Validation errors
- 🔄 Duplicate entries
- 🚫 Authentication failures
- 💽 Database errors
- 🔧 Server errors
- ⚡ Express.js
- 🍃 MongoDB with Mongoose
- 🎟️ JSON Web Tokens (JWT)
- 🔐 bcrypt