A secure and scalable URL Shortener Backend API built with Node.js, Express, TypeScript, MySQL, Redis, and Zod. This project provides authentication, role-based authorization, rate limiting, login protection, and URL shortening core logic.
- User Registration & Login
- JWT Authentication
- Refresh Token System
- Logout & Token Revocation
- Login Attempt Prevention
- Rate Limiting
- Password Update & Account Removal
- Role-Based Access Control (Owner / Admin / User)
- Register & Login Users
- Get Current Profile
- Fetch Users
- Update User Role (Admin)
- Delete Users (Admin / Owner)
- Secure Password Update
- Create Short URL
- Redirect to Original URL
- Soft Authentication Support
- Admin URL Deletion
- IP Country Detection (GeoIP)
- Express v5
- TypeScript
- Zod Validation Middleware
- Redis Integration
- MySQL Database
- Environment-based Configuration
Backend
- Node.js
- Express.js
- TypeScript
Database & Storage
- MySQL
- Redis
Security
- JWT
- Bcrypt
- Rate Limiting
- Login Protection
Validation
- Zod
Utilities
- GeoIP2 (IP Country Detection)
src/
├── modules/
│ ├── user/
│ └── url/
├── middlewares/
├── config/
├── utils/
└── server.ts
git clone https://github.com/imranmalakzai/shortly-flags-API
cd shortly-flags-API
npm install
Rename:
.env.production.tmp
to:
.env.production
Example variables:
PORT=5000
JWT_SECRET=your_secret
DB_HOST=localhost
DB_USER=root
DB_PASS=password
DB_NAME=shortly
REDIS_URL=redis://localhost:6379
Install locally:
- Node.js (v18+ recommended)
- MySQL Server
- Redis Server (Windows supported)
npm run dev
npm run build
npm start
/api/
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register | Register user |
| POST | /api/auth/login | Login |
| POST | /api/auth/refresh | Refresh token |
| DELETE | /api/auth/logout | Logout |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/users | Get all users |
| GET | /api/users/:userId | Get user |
| GET | /api/users/me | Current profile |
| DELETE | /api/users/me | Delete account |
| PATCH | /api/users/me/password | Update password |
| Method | Endpoint | Description |
|---|---|---|
| PATCH | /api/admin/users/:userId | Update role |
| DELETE | /api/admin/users/:userId | Delete user |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/ | Create short URL |
| GET | /api/:shortCode | Redirect to original URL |
| DELETE | /api/urlId | Delete URL (Admin) |
- Rate Limiting
- Login Attempt Tracking
- Password Hashing
- Role Authorization Middleware
- Request Validation via Zod
- Secure JWT Authentication
All request bodies are validated using:
Zod + Custom Validation Middleware
Contributions are welcome.
Steps:
- Fork the repository
- Create a feature branch
- Commit changes
- Open a Pull Request
MIT License
Imran Malakzai Backend Developer