A secure and scalable RESTful API built with Node.js and Express, designed for shortening URLs and handling user authentication using JWT and refresh tokens.
- User registration and login with hashed passwords
- JWT-based authentication
- Refresh token support via HTTP-only cookies
- URL shortening and redirection
- Node.js – Server-side JavaScript runtime
- Express – Web framework for building the API
- MongoDB + Mongoose – Database and ODM
- JWT (JSON Web Tokens) – Authentication
- bcrypt – Password hashing
- HTTP-only Cookies – Refresh token storage
-
Clone the repository
git clone <repository-url> cd <project-folder>
-
Install dependencies
npm install
-
Configure MongoDB
Ensure MongoDB is running locally or set up a remote database (e.g. MongoDB Atlas).
-
Start the server
node index.js
Example development secrets:
const ACCESS_SECRET = "access123";
const REFRESH_SECRET = "refresh";Use strong, secure values for production.
Registers a new user.
Request Body:
{
"username": "testuser",
"password": "123456"
}Authenticates a user and issues an access token (in response) and a refresh token (in HTTP-only cookie).
Request Body:
{
"username": "testuser",
"password": "123456"
}Generates a new access token using a valid refresh token stored in cookies.
Creates a short URL. Requires authentication.
Headers:
Authorization: Bearer <access_token>
Request Body:
{
"originalUrl": "https://example.com"
}Redirects to the original URL using the short ID.
- Routes like
/urlare protected and require a valid JWT access token. - Refresh tokens are securely handled via HTTP-only cookies.
- Passwords are hashed with
bcryptbefore being stored. - This API follows REST principles and is ready for frontend or mobile integration.