← Back to Portfolio

URL Shortener API

A secure and user-based URL shortening service built with Node.js, Express.js, MongoDB, JWT authentication, and Google OAuth 2.0, allowing users to create and manage their own shortened URLs.

Type REST API (backend)
Status Completed
Stack Node.js, Express.js, MongoDB, Passport.js
Auth JWT, HTTP-only Cookie, Google OAuth 2.0

Overview

URL Shortener API is a secure and user-based URL shortening service that converts long URLs into short, easy-to-share links. Authenticated users can create and manage their own shortened URLs.

The project was built to understand how a realistic backend service works beyond basic CRUD operations, with a focus on REST API development, authentication, protected routes, validation, unique URL generation, HTTP redirection and frontend-backend communication.

Key features

  • User registration: users can create an account.
  • User login: users can authenticate using their credentials.
  • Google sign-in: users can log in or sign up with their Google account using OAuth 2.0 and Passport.js.
  • Automatic account creation: a new account is created on first Google login using the verified Google email.
  • Password hashing: passwords are securely hashed using bcrypt.
  • JWT authentication: JWT is used to authenticate users.
  • HTTP-only cookies: authentication tokens are stored in HTTP-only cookies.
  • Protected routes: private API endpoints require authentication.
  • URL shortening: authenticated users can convert long URLs into short links.
  • Unique short identifiers: the system generates unique identifiers for shortened URLs.
  • URL history: users can view their previously created URLs.
  • URL count: the dashboard displays the number of URLs created by the user.
  • Short URL redirection: short URLs redirect users to their original URLs.
  • Request validation: incoming requests are validated on the backend.
  • User-specific URLs: each user can access their own URL history.
  • Copy shortened URLs: users can copy their generated short URLs from the dashboard.

How URL shortening works

  • An authenticated user submits a long URL.
  • The request is sent to POST /shortner.
  • The server generates a unique short identifier.
  • The URL information is saved in MongoDB.
  • The generated short URL is returned to the user.
  • When the short URL is visited, the server finds the corresponding original URL.
  • The server redirects the request to the original URL.

Authentication flow

  • A user logs in with their credentials.
  • The server validates the credentials.
  • A JWT is generated after successful authentication.
  • The JWT is stored in an HTTP-only cookie.
  • Protected requests pass through authentication middleware.
  • Authenticated user identity is used when accessing user-specific URLs.

Google OAuth 2.0 login

  • The user clicks Continue with Google, which opens GET /auth/google.
  • Passport.js redirects the user to Google's consent screen.
  • After approval, Google redirects back to GET /auth/google/callback.
  • The server checks that the Google email is verified, then finds the user in MongoDB or creates a new one.
  • The same JWT is issued in an HTTP-only cookie, so both login methods use the same protected routes.

Example endpoints

The API is organized around authentication and URL management. The following are the endpoints documented in the project.

Method Endpoint Purpose
POST /register Register a new user
POST /login Login user
POST /logout Logout the authenticated user
GET /auth/google Start Google OAuth login
GET /auth/google/callback Handle Google's response, set the JWT cookie and redirect to the dashboard
GET /me Get the current authenticated user
POST /shortner Create a short URL
GET /showAll Get the user's URLs
GET /:shortLink Redirect to the original URL

What I learned

  • Building REST APIs with Node.js and Express.js
  • JWT authentication with HTTP-only cookies
  • Google OAuth 2.0 login with Passport.js
  • Combining third-party login with my own JWT authentication
  • Password hashing with bcrypt
  • Authentication middleware and protected routes
  • MongoDB and Mongoose for data management
  • Request validation and error handling
  • Generating unique short URLs
  • Associating URLs with authenticated users
  • Implementing short URL redirection
  • Connecting a frontend with a backend API