Skip to content

Rajdip1/own_auth_system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🔐 Authentication System – Project Summary (Part 1 + Part 2)


📌 Tech Stack

Frontend

  • React (Vite + TypeScript)
  • Axios (interceptors)
  • Chakra UI

Backend

  • Node.js
  • Express
  • MongoDB

Security

  • JWT (Access + Refresh Token split)
  • bcrypt password hashing
  • Rate limiting
  • HttpOnly cookies

✅ Part 1 – Core Authentication System

1️⃣ User Signup

  • Validate user existence
  • Hash password using bcrypt
  • Store hashed password in MongoDB

2️⃣ User Login

  • Compare password using bcrypt

  • Generate:

    • Access Token (short-lived)
    • Refresh Token (long-lived)

3️⃣ Protected Route

  • Middleware verifies access token
  • Attaches req.userId
  • Returns 401 if invalid or expired

✅ Part 2 – Production-Level Security Upgrade

🔹 1. Rate Limiting

  • Applied on login route
  • Prevents brute-force attacks
  • Returns 429 when exceeded

🔹 2. Access + Refresh Token Architecture

Access Token

  • Short expiry (e.g., 30s)
  • Sent via Authorization header
  • Stored in localStorage

Refresh Token

  • Longer expiry (e.g., 5m)
  • Stored in HttpOnly cookie
  • Not accessible via JavaScript

🔹 3. Axios Interceptors

Request Interceptor

  • Automatically attaches access token to headers

Response Interceptor

  • If 401:

    • Calls /auth/refresh
    • Gets new access token
    • Retries original request
  • Prevents infinite refresh loops


🔹 4. Secure Cookie Configuration

Backend:

  • httpOnly: true
  • sameSite: 'lax'
  • secure: false (dev)
  • withCredentials: true enabled

Frontend:

  • Axios configured with withCredentials: true

🔹 5. Session Persistence

On page refresh:

  • Access token checked
  • /profile verified
  • Silent refresh triggered if expired
  • User remains logged in

🔹 6. Logout Flow (Production Style)

Frontend:

  • Calls /auth/logout
  • Removes access token

Backend:

  • Clears refresh cookie

🧠 What This Architecture Achieves

✔ Secure token lifecycle management ✔ Silent access token renewal ✔ HttpOnly refresh token security ✔ Brute-force protection ✔ Clean logout handling ✔ Stable session behavior ✔ No infinite refresh loops


🏆 Current Level of System

This is:

  • Beyond tutorial-level JWT auth
  • Production-ready for small SaaS
  • Interview-ready authentication architecture
  • Mid-level backend engineering implementation

🚀 Potential Next Upgrades (Part 3+)

  • Refresh token rotation
  • Store refresh tokens in database
  • Logout all devices
  • Session/device tracking
  • Role-based access control (RBAC)
  • CSRF protection
  • Monitoring & suspicious login detection

📈 Project Status

Part 1: Completed ✅ Part 2: Completed ✅

System is stable, secure, and production-aligned.

About

Project goal is to learn how the custom auth system works

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages