User Data Schema
Relevant source files
The following files were used as context for generating this wiki page:
The User Data Schema defines the structure and behavior of user entities within the Bridge AI platform. It serves as the foundation for authentication, profile management, and relational links to other modules such as chat history and session management. The schema is implemented using Mongoose for MongoDB and includes built-in hooks for security measures like password hashing.
This schema facilitates core functionalities including user registration, secure login via JSON Web Tokens (JWT), and profile updates. It also establishes the necessary references for the Chat Model to associate conversations with specific individuals.
Core Schema Structure
The User model is defined with a comprehensive set of fields that balance identifying information with security requirements. It utilizes Mongoose Schema with timestamps enabled to automatically track creation and update times.
Field Definitions
| Field | Type | Constraints | Description |
|---|---|---|---|
username | String | Required, Unique, Lowercase, Trim, Index | A unique identifier used for login and profile indexing. |
email | String | Required, Unique, Lowercase, Trim | The user's primary contact and login identifier. |
fullName | String | Required, Trim, Index | The display name of the user. |
password | String | Required | A hashed representation of the user's password. |
refreshToken | String | Optional | A token used to generate new access tokens without re-authenticating. |
createdAt | Date | Auto-generated | Timestamp of account creation. |
updatedAt | Date | Auto-generated | Timestamp of the last profile or status change. |
Sources: BACKEND/src/models/User.model.js:5-39
Relationship Diagram
The following Entity-Relationship diagram illustrates how the User entity serves as a central node for other system modules like the Chat system.
Security and Authentication Logic
The schema is not just a passive data structure; it includes active middleware and methods to handle security.
Middleware and Methods
- Pre-save Hook: Automatically intercepts the save process to hash passwords using
bcryptwith a salt factor of 10 if the password field has been modified. - Password Verification: A method
isPasswordCorrectprovides a secure way to compare plain-text login attempts with hashed database entries. - Token Generation: Dedicated methods handle the creation of
AccessTokens(containing profile payload) andRefreshTokens(containing only the ID).
Sources: BACKEND/src/models/User.model.js:41-79
Authentication Flow
The following sequence diagram demonstrates the interaction between the controller and the model during the login process.
Data Interaction and Modification
User data is interacted with through various frontend components that map to backend controller actions.
Registration and Profile Updates
The registration process requires a full payload of user data, while updates may target specific fields like fullName and email.
- Registration: Validates that no existing user shares the same username or email before creation.
- Update: Updates account details via
findByIdAndUpdateand returns the updated document excluding sensitive fields like the password.
Sources: BACKEND/src/controllers/user.controller.js:56-86, BACKEND/src/controllers/user.controller.js:189-211
Frontend Implementation Summary
| Component | Files | Key Data Handled |
|---|---|---|
| Signup Page | signup.jsx | username, fullName, email, password |
| Edit Profile | editprofilepage.jsx | username, fullName, email, password (optional) |
Sources: frontend/front/src/signup/signup.jsx:9-15, frontend/front/src/editprofilepage/editprofilepage.jsx:5-9
// Example of field validation in registration
if ([fullName, email, username, password].some((field) => field?.trim() === "")) {
throw new ApiError(400, "All fields are required");
}
Sources: BACKEND/src/controllers/user.controller.js:59-63
Summary
The User Data Schema provides a robust framework for managing identities within the project. By combining Mongoose schema definitions with integrated JWT generation and bcrypt hashing, it ensures that user information is both structured for performance (via indexing) and secured against unauthorized access. This schema is the authoritative source for user-related data across the entire backend architecture.