Skip to content

๐Ÿ” Built from scratch for mission-critical systems like banking, healthcare, and government infra, this Strict Auth Service uses PostgreSQL + Prisma ORM with device-level session control, hashed access tokens, short-lived JWTs, revocation capability, and a fully scalable architecture.

Notifications You must be signed in to change notification settings

YatharthKumarSaxena/Strict-Auth-Service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

74 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“˜ Strict Authentication Service

Welcome to the Strict Authentication Service, a secure and scalable backend authentication system engineered using Node.js, Express.js, and PostgreSQL, designed with industry-grade practices such as Rate Limiting, Single-Device Tracking, and Token Rotation (JWT). This project is structured with modularity, reusability, and production-readiness in mind, powered by real-world system design and software engineering principles.


๐Ÿงญ Table of Contents


๐Ÿ“– Introduction

This monolithic authentication backend is engineered to support:

  • Secure account creation with single active session per user
  • Refresh and access token generation with custom expiry
  • Custom JWT validation middleware
  • Admin auto-creation at server start
  • Per-device rate limiting for brute-force prevention
  • Intelligent logging for each action (e.g., sign in, sign out, register)
  • APIs to block or unblock device access manually (admin-controlled)

The entry point for this project is server.js, where all major components are initialized including:

  • PostgreSQL connection
  • Admin account bootstrap
  • Centralized route mounting
  • Global error and 404 handling
  • Cron job triggers

๐Ÿงช Features

๐Ÿ”น Feature โœ… Implemented
Single-Device Login Enforcement โœ”๏ธ
JWT Token Generation โœ”๏ธ
Rate Limiting per Device & Route โœ”๏ธ
Admin Auto-Creation at Startup โœ”๏ธ
Authentication Logs โœ”๏ธ
Centralized Error Handling โœ”๏ธ
Cookie Parser + JSON Body Parsing โœ”๏ธ
Cron Job Integration โœ”๏ธ
Intelligent 404 Rate Monitoring โœ”๏ธ
Block/Unblock Device API โœ”๏ธ

๐Ÿ”ง Tech Stack

  • Node.js: Runtime
  • Express.js: Web framework
  • PostgreSQL + Prisma: NoSQL DB with schema modeling
  • JWT: Token-based authentication
  • dotenv: Environment config management
  • cookie-parser: For handling cookies
  • uuid: For generating deviceID

๐Ÿง  Design Principles & Patterns

โœ… Design Principles

Principle Full Form Usage
SOLID Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, Dependency Inversion Modular services, controllers, utils follow SRP
DRY Donโ€™t Repeat Yourself Reused logic across tokens, logging, error handlers
KISS Keep It Simple, Stupid Clear token logic, admin setup kept straightforward
YAGNI You Arenโ€™t Gonna Need It Avoided premature scaling or over-engineering

โœ… Design Patterns Used

Pattern Purpose
Singleton MongoDB connection, express server bootstrap
Factory Token generation utility with expiry injection
Template Method Used in log writing & token validation flow (extendable blueprint)
Observer (simulated) Setup ready for future log-subscriber or event-bus design

๐Ÿงฑ Folder Structure

๐Ÿ“ฆ Total 12 folders and 5 files are present in this project directory.

๐Ÿ“ Folders

Folder Name Description
๐Ÿ“ rate-limiters/ Device & route-specific rate limit services
๐Ÿ“ controllers/ Handles business logic for auth routes
๐Ÿ“ configs/ Configuration files for DB, server, tokens
๐Ÿ“ middlewares/ JWT auth, global error, malformed JSON etc.
๐Ÿ“ utils/ Utility functions like token creation, logs
๐Ÿ“ services/ Handles internal logic like limiter service
๐Ÿ“ routes/ Route entry-point for all modules
๐Ÿ“ prisma/ Prisma schema and database access layer
๐Ÿ“ cron-jobs/ Server-triggered tasks on boot
๐Ÿ“ internal-calls/ For internal microservice/API interaction
๐Ÿ“ node_modules/ Auto-installed dependencies
๐Ÿ“ clients/ Includes clients that interact with Database

๐Ÿ“„ Files

File Name Description
server.js ๐ŸŽฏ Entry point โ€” starts DB, routes, admin
.env.example ๐ŸŒ Sample environment configuration
package.json ๐Ÿ“ฆ Project metadata and script definitions
package-lock.json ๐Ÿ”’ Locked dependency tree for consistent build
.gitignore ๐Ÿšซ Files/directories ignored in version control

๐Ÿ› ๏ธ Environment Variables

(Same as earlier โ€” no changes required unless new secrets are introduced for block/unblock APIs.)


๐Ÿšฆ Rate Limiting Logic

The logic remains focused on device-based tracking, but now it enforces single device session per user. If the user tries to log in from another device, the older one is invalidated unless explicitly logged out.

Also:

  • Devices can be blocked manually by the admin using the block device API.
  • Blocked devices are prevented from making any further requests unless unblocked.

DeviceID plays a critical role in both rate limiting and access enforcement. DeviceID + UserID combo is also used in Rate Limiting where Token is Required


๐Ÿงช Testing Strategy

In addition to earlier cases, following have been tested:

๐Ÿ“› Device Blocking Tests:

  • โœ… Access denied from blocked device
  • โœ… Unblock restores access
  • โœ… Attempt to use access token from blocked device is rejected

๐Ÿงฌ Planned Enhancements

๐Ÿ”ฎ Enhancement Priority Notes
Role-Based Access Control (RBAC) ๐Ÿ”บ High Allow role assignment to users (e.g., ADMIN, MODERATOR, USER)
OTP-Based Authentication ๐Ÿ”บ High Add SMS/Email-based OTPs for passwordless login
API Rate Limits per User Tier ๐Ÿ”ธ Medium Different limits for free vs. premium accounts
PostgreSQL Index Optimization ๐Ÿ”ธ Medium Speed up query performance and reduce latency
Test Automation ๐Ÿ”น Low Integrate with CI/CD for robust test coverage

๐ŸŽฏ Final Takeaway

This updated Strict Auth backend narrows down to a single-device secure session system, eliminating ambiguity and enabling administrators to control device access through direct APIs. It simulates the authentication needs of banking apps, sensitive enterprise tools, or IoT platforms, where session control and device-level trust are paramount.

๐Ÿ’ก What Youโ€™ll Learn from This Repo:

  • How to restrict user login to one device at a time
  • How to implement manual device blocking logic
  • How to scale secure auth systems for production
  • How to track request behavior route-by-route per device
  • How to enforce token validity even after logout or block

๐Ÿ“ Designed & Engineered with Precision by Yatharth Kumar Saxena ๐Ÿš€

About

๐Ÿ” Built from scratch for mission-critical systems like banking, healthcare, and government infra, this Strict Auth Service uses PostgreSQL + Prisma ORM with device-level session control, hashed access tokens, short-lived JWTs, revocation capability, and a fully scalable architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published