Back to Projects

Authentication Service

Authentication Service
ExpressJWTSecurity

"The invisible shield behind every great application."


The Authentication Service is a standalone, purely backend microservice designed to handle user registration, secure login, and session management. Built from scratch using Node.js and Express, it acts as a highly secure, plug-and-play gatekeeper for any modern web application.


Problem Statement

"Never trust a black box with your security."


Authentication is required in almost every application. Today, it’s incredibly easy to just plug in a third-party service like Firebase or Auth0 and call it a day.

But relying entirely on black-box solutions leaves a developer blind. If you don't know how a JSON Web Token (JWT) is constructed, how passwords are salted, or how cookies are secured against cross-site scripting (XSS), you don't really understand backend security.

The goal was to strip away the abstractions and build a complete authentication system from the ground up.

The Challenge

  • πŸ” Password Cryptography β€” Securely hashing and salting user passwords before they ever touch the database.
  • 🎟️ Token Management β€” Generating, validating, and securely transmitting JWTs for stateless authentication.
  • πŸ”„ Access & Refresh Tokens β€” Implementing a dual-token architecture to balance long-term user sessions with high security.
  • πŸͺ Cookie Security β€” Understanding how to securely send tokens via HTTP-only cookies to prevent malicious scripts from stealing credentials.

The Solution

"A robust, production-ready security microservice."


I built a complete REST API dedicated entirely to managing user identities. It abstracts the authentication logic away from the main application, making it a scalable, reusable microservice.

Register β†’ Hash β†’ Authenticate β†’ Tokenize β†’ Verify

  • Bcrypt Hashing β€” Passwords are computationally hashed, ensuring that even in the event of a database breach, user credentials remain safe.
  • Stateless Authorization β€” Utilizes JWTs, meaning the server doesn't need to look up a session in the database for every single request, drastically improving performance.
  • Refresh Token Rotation β€” If an access token expires, the system securely issues a new one without forcing the user to log in again, provided their refresh token is valid.
  • Middleware Protection β€” Custom Express middleware that intercepts requests, verifies tokens, and protects private API routes automatically.

An uncrackable gatekeeper designed to protect user data.


Technology Stack

"Pure Backend Engineering"


Server & Routing:

Node.js + Express.js provide the lightweight, blazing-fast runtime and routing necessary for handling thousands of concurrent authentication requests.

Security & Cryptography:

Bcrypt.js for password hashing, and JSON Web Tokens (JWT) for stateless, secure data transmission.

Database:

MongoDB efficiently stores user profiles, encrypted passwords, and active refresh tokens.

A focused, security-first backend architecture.


The Authentication Story

"Looking under the hood."


This project holds a very special place in my backend journey.

It started when I joined the Chai Aur Code cohort. Up until that point, I had built applications where authentication was just a small piece of a much larger puzzle. I knew how to make a login route work, but I wanted to go deeper.

The cohort completely shifted my perspective from building "just another app" to writing "production-grade backend systems."

I realized that to be a true backend engineer, I had to stop relying on magic. I needed to build the magic myself.

So, I decided to dedicate an entire project solely to mastering the authentication flow.


πŸ”’ CHAPTER 01 β€” THE VAULT

"Hashing, salting, and cryptography."


The first rule of backend development: Never store plain-text passwords.

I started by integrating Bcrypt. I learned about saltingβ€”adding random data to the password before hashing it so that two users with the same password would have completely different hashes in the database.

It was fascinating to see a simple password like password123 transform into a massive, unrecognizable cryptographic string. I finally understood how large companies secure their databases.


🎟️ CHAPTER 02 β€” THE DUAL-TOKEN DILEMMA

"Why one token is never enough."


Initially, I just generated a JWT upon login and sent it to the user. But then the cohort challenged me:

"What happens if that token is stolen? How long does it last?"

If the token lasts forever, a hacker has permanent access. If it expires in 15 minutes, the user has to log in constantly, which ruins the user experience.

The solution was the Access and Refresh Token Architecture.

I built a system where the Access token expires very quickly (e.g., 15 minutes) for high security, but the Refresh token lasts for days and is stored in a highly secure HTTP-only cookie. When the Access token dies, the server silently uses the Refresh token to issue a new one behind the scenes.

Implementing this flow successfully was a massive "Aha!" moment for me.


πŸͺ CHAPTER 03 β€” THE COOKIE MONSTER

"Locking the doors against XSS."


Where do you store tokens? LocalStorage? SessionStorage?

I learned the hard way that storing tokens in LocalStorage leaves them vulnerable to Cross-Site Scripting (XSS) attacks. Any malicious JavaScript on the page can steal them.

I had to dive deep into cookie policies. I configured my Express server to send tokens inside HttpOnly, Secure, and SameSite cookies. This meant the browser would hold the token securely, automatically attach it to requests, and absolutely no client-side JavaScript could ever touch it.


πŸš€ CHAPTER 04 β€” PLUG AND PLAY

"From cohort exercise to production template."


What started as a learning exercise in the Chai Aur Code cohort became one of my most valuable repositories.

I didn't just build a login system; I built a reusable authentication microservice. Because I abstracted all the logic cleanly, I now had a bulletproof template that I could drop into literally any future Node.js project.

The project that taught me the true meaning of backend security.

Comments -