User Authentication Features
Relevant source files
The following files were used as context for generating this wiki page:
- marketplace-frontend/src/app/services/root/authorization.service.ts
- marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts
- marketplace-backend/app/src/repositories/impl/customers.repository.ts
- marketplace-backend/app/src/schemas/customer.schema.ts
- marketplace-backend/app/src/modules/auth/auth.module.ts
- marketplace-frontend/src/app/constants/validators.ts
- marketplace-backend/app/src/dto/requests.dto.ts
The User Authentication Features manage identity verification and session persistence within the Marketplace application. The system provides registration, login, logout, and session renewal capabilities using JSON Web Tokens (JWT) and server-side caching. This module coordinates between the Angular frontend and the NestJS backend to ensure secure access to customer accounts.
Sources: marketplace-backend/app/src/modules/auth/auth.module.ts, marketplace-frontend/src/app/services/root/authorization.service.ts
System Architecture
The authentication architecture utilizes a NestJS backend module that integrates JWT for session management and Mongoose for data persistence. The frontend interacts with this system through an AuthorizationService that handles HTTP requests and session state updates.
Backend Authentication Module
The AuthModule configures the security infrastructure:
- JWT Module: Handles token generation and verification using a secret key and expiration settings.
- Cache Module: Stores customer information to reduce database load.
- Mongoose Module: Manages the
CustomerandRoleschemas.
Sources: marketplace-backend/app/src/modules/auth/auth.module.ts:14-55
Customer Data Model
The system stores customer credentials and profile information using a structured schema.
| Field | Type | Constraints |
|---|---|---|
id | String | Unique, Required |
userName | String | Unique, Required |
email | String | Unique, Required |
password | String | Required (Hashed) |
firstName | String | Required |
age | Number | Default: 18 |
role | ObjectId | Reference to Role |
Sources: marketplace-backend/app/src/schemas/customer.schema.ts:16-37, marketplace-backend/app/src/repositories/impl/customers.repository.ts:25
Authentication Flows
Login Process
The login flow validates credentials against the database and establishes a session.
Registration Process
The registration process creates a new user, hashes the password, and assigns a default role.
- Frontend Validation: Applies regex patterns to
userNameandpassword. - Request Handling: The
AuthControllerreceives theRegisterRequestDto. - Persistence: The
CustomersRepositorygenerates a UUID, hashes the password usingbcrypt, and saves the document. - Session Establishment: The system generates a JWT and returns the customer profile.
Sources: marketplace-backend/app/src/repositories/impl/customers.repository.ts:18-30, marketplace-frontend/src/app/constants/validators.ts:1-8
Session Management
The system manages sessions through HTTP-only cookies and a server-side cache.
Token Configuration
The backend sets a JWT in a cookie with specific security options to prevent client-side access.
const COOKIE_OPTIONS = {
httpOnly: true,
maxAge: 60 * 60 * 24 * 30, // 30 days
path: process.env.COOKIE_PATH,
domain: process.env.DOMAIN,
};
Sources: marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts:14-19
Cache Strategy
The AuthController caches customer DTOs using a prefix id_ to speed up subsequent requests. When getCustomerInfoFromJWT is called, the system first checks the _cacheManager. If the data exists, it returns it; otherwise, it queries the database and populates the cache.
Sources: marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts:32-49
Logout and Cleanup
The logout process invalidates the session by performing two actions:
- Removes the customer entry from the server-side cache.
- Clears the JWT cookie from the client response.
Sources: marketplace-backend/app/src/modules/auth/controllers/auth.controller.ts:75-81
Client-Side Security
The frontend AuthorizationService maintains the authentication state using ReplaySubject observables.
| Observable | Description |
|---|---|
onCustomerInfoChanged$ | Emits the Customer object when profile data is updated. |
onAuthStatusChanged$ | Emits a boolean representing the current login state. |
Sources: marketplace-frontend/src/app/services/root/authorization.service.ts:16-25
Validation Constants
The frontend enforces strict validation rules for user input before submitting authentication requests.
| Validator | Pattern / Constraint |
|---|---|
USERNAME_VALIDATOR | 8-16 chars, alphanumeric, no consecutive dots/underscores. |
PASSWORD_VALIDATOR | Requires at least one number, uppercase, and lowercase letter. |
PASSWORD_MIN_LENGTH | 8 |
FIRST_NAME_LENGTH | 20 |
Sources: marketplace-frontend/src/app/constants/validators.ts:1-11
Summary
The User Authentication Features provide a secure framework for identity management. By combining JWT-based sessions, bcrypt password hashing, and server-side caching, the application ensures both security and performance. The frontend AuthorizationService serves as the single point of contact for managing user state and interacting with the backend authentication endpoints.