API & Class Design for Sign-in / Register Page

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-26

Problem Statement

Design the Low-Level Design (LLD) and API structure for a User Authentication module that powers a Sign-in and Register Page for a web or mobile application (like any modern app — e.g., Instagram, Swiggy, or LinkedIn).


The system should allow new users to register and existing users to sign in securely, while maintaining extensibility for future authentication features like OTP login, social login (Google/Facebook), and password reset.


🎯 Goal


Design the classes, APIs, and data flow for:


•User registration (sign-up)
•User sign-in (login)
•Token-based session management
•Password validation and hashing
•Error handling and response structure


✅ Functional Requirements


1. User Registration (Sign-Up)


•User provides:
•Name
•Email / Mobile number (unique identifier)
•Password
•System should:
•Validate email/phone format and password strength.
•Check if the user already exists.
•Store user credentials securely (hashed password).
•Send verification (email/SMS) for confirmation (optional).


2. User Login (Sign-In)


•User provides:
•Email / Mobile number
•Password
•System should:
•Validate credentials against stored records.
•Generate an access token (e.g., JWT) upon success.
•Allow subsequent API calls using the token.
•Return errors for invalid or inactive accounts.


3. Session / Token Management


•System should:
•Issue tokens with expiry (e.g., 30 mins).
•Refresh tokens (optional extension).
•Invalidate tokens on logout or password change.


4. Error Handling


•Return proper error codes and messages for:
•Invalid input
•Authentication failure
•Duplicate registration
•Token expiration


⚙️ Non-Functional Requirements


•Secure password handling — use hashing (e.g., bcrypt, SHA-256).
•Thread-safe session storage for concurrent users.
•Extensible design to support future login methods (Google, Apple ID, OTP).
•Rate limiting for brute-force login attempts (can be abstracted for now).


🧱 Key Classes to Design


ClassResponsibility


User


Represents an application user (stores name, email, hashed password, status).


Credentials


Handles password hashing, verification, and comparison.


AuthService


Core service for registration, login, and token management.


TokenManager


Generates, validates, and expires access tokens.


UserRepository


Manages CRUD operations on user data (simulate DB).


ValidationService


Validates input formats and password strength.


AuthController (API Layer)


Handles REST endpoints for sign-in/sign-up.


ErrorResponse / ApiResponse


Standardized response wrapper for APIs.


🧩 Example Class Relationships

1 2User 3 ├── id 4 ├── name 5 ├── email 6 ├── hashedPassword 7 ├── status (ACTIVE / PENDING_VERIFICATION / DISABLED) 8 ├── createdAt 9 ├── updatedAt 10 11AuthService 12 ├── registerUser(name, email, password) 13 ├── login(email, password) 14 ├── logout(token) 15 16TokenManager 17 ├── generateToken(userId) 18 ├── validateToken(token) 19 ├── invalidateToken(token)


🌐 Example API Endpoints


EndpointMethodDescription

/api/v1/auth/register


POST


Register a new user

/api/v1/auth/login


POST


Sign in with credentials

/api/v1/auth/logout


POST


Invalidate session token

/api/v1/auth/verify


GET

Verify email/OTP (optional)


🧪 Sample API Flow


🔸 Register Request:

1 2POST /api/v1/auth/register 3{ 4 "name": "Sahil Batra", 5 "email": "sahil@example.com", 6 "password": "Strong@123" 7}

✅ Response:

1 2{ 3 "status": "success", 4 "message": "User registered successfully", 5 "userId": "U10239" 6}

🔸 Login Request:

1 2POST /api/v1/auth/login 3{ 4 "email": "sahil@example.com", 5 "password": "Strong@123" 6}

✅ Response:

1 2{ 3 "status": "success", 4 "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." 5}

🔸 Invalid Login:

1 2{ 3 "status": "error", 4 "error": "Invalid credentials" 5}


🔐 Security & Concurrency


•Passwords are hashed + salted before storing.
•Tokens are stateless (JWT) or stored in Redis for validation.
•Concurrent login attempts handled via atomic token updates.


🧠 Possible Extensions


•Add multi-factor authentication (MFA) via OTP.
•Add social login (Google, Apple, Facebook).
•Add password reset / forgot password flow.
•Add rate limiting for login attempts.


✅ Final Summary:


Design an API and class structure for Sign-in / Register functionality that supports:


•User registration and login,
•Secure credential management,
•Token-based session handling,
•Extensible architecture for future auth mechanisms.


Focus on OOP modeling, authentication flow, and API contract design, not UI or database scaling.

Solution

Comments