Overview
TheUser struct represents a user account in the system. It is stored in the MongoDB users collection and includes authentication credentials, profile information, and metadata.
User Struct Definition
backend/users/model.go:12-22
Fields
ID
Unique user identifier (UUID v4 format)
- JSON key:
id - MongoDB field:
_id - Format: UUID v4 (e.g.,
a1b2c3d4-e5f6-7890-abcd-ef1234567890) - Generated: Automatically during registration using
uuid.New().String()
User’s email address (unique)
- JSON key:
email - MongoDB field:
email - Constraints: Must be unique across all users
- Used for: Login authentication, account identification
Phone
User’s phone number
- JSON key:
phone - MongoDB field:
phone - Format: No specific format enforced in model
- Used for: Alternative lookup method
Password
Hashed password (bcrypt)
- JSON key:
password - MongoDB field:
password - Storage: bcrypt hash with cost factor 14
- Security: Never stored in plain text, never returned in API responses
Role
User’s role for authorization
- JSON key:
role - MongoDB field:
role - Common values:
admin,user,moderator(not enforced by model) - Default: Not set during registration
Status
User account status
- JSON key:
status - MongoDB field:
status - Valid values:
pending: Newly registered, awaiting verificationactive: Account is active and can log in- Custom statuses:
suspended,banned, etc.
- Default:
"pending"for new registrations - Login requirement: Must be
"active"to log in
AccountType
Type of user account
- JSON key:
account_type - MongoDB field:
account_type - Common values:
free,premium,enterprise(not enforced by model) - Default: Not set during registration
CreatedAt
Account creation timestamp
- JSON key:
created_at - MongoDB field:
created_at - Format: Go
time.Time, serialized as RFC3339 in JSON - Set: Automatically during registration using
time.Now()
UpdatedAt
Last update timestamp
- JSON key:
updated_at - MongoDB field:
updated_at - Format: Go
time.Time, serialized as RFC3339 in JSON - Set: Should be updated on user modifications (not automatically handled)
Struct Tags
Each field includes multiple struct tags for different frameworks:json: Field name in JSON serializationbson: Field name in MongoDB documentsquery: Query parameter binding (Echo framework)form: Form data binding (Echo framework)param: URL parameter binding (Echo framework)
MongoDB Collection
Users are stored in theusers collection with the following characteristics:
- Database: Default database from
MONGODB_URI - Collection name:
users - Primary key:
_id(maps toUser.ID) - Indexes: Email should be unique (enforce at database level)
Example User Document
In Go
In MongoDB
In JSON API Response
User Creation Defaults
When a user registers, the following defaults are applied:| Field | Default Value |
|---|---|
| ID | uuid.New().String() |
| User provided | |
| Phone | Not set |
| Password | bcrypt hash of user’s password |
| Role | Not set |
| Status | "pending" |
| AccountType | Not set |
| CreatedAt | time.Now() |
| UpdatedAt | Not set |
backend/auth/controller.go:30-48
Validation
The model itself does not include validation tags. Validation should be performed at the handler level:- Email: Check format, uniqueness
- Password: Minimum length, complexity requirements
- Phone: Format validation if required
- Status: Validate against allowed values
Security Best Practices
-
Password Handling
- Always hash passwords with bcrypt before storing
- Never return password in API responses
- Use cost factor 14 or higher
-
Email Privacy
- Consider email visibility in public profiles
- Implement email change verification
-
Status Enforcement
- Check status before allowing login
- Implement status change audit logs
-
Phone Verification
- Verify phone numbers before allowing phone-based operations
- Implement OTP for phone changes
Related Documentation
- User Operations - CRUD functions for User model
- POST /register - Create new user
- POST /login - Authenticate user