Overview
Horse Trust uses a secure authentication system built on JWT (JSON Web Tokens) and bcrypt password hashing. The platform supports two user roles: sellers and admins, with email and phone verification capabilities.User Model
The authentication system is built around a comprehensive User model defined inserver/src/models/User.ts:
Key Features
Password Security
Passwords are hashed using bcrypt with configurable salt rounds before storage
Email Validation
Built-in regex validation ensures proper email format
Phone Verification
International phone format validation with E.164 standard
Role-Based Access
Two-tier role system: sellers and admins
Password Hashing
Passwords are automatically hashed before saving using a pre-save middleware hook:The system uses 12 salt rounds by default, configurable via the
BCRYPT_SALT_ROUNDS environment variable.Login Flow
Frontend Implementation
The login page (client/app/login/page.tsx) provides a clean, responsive interface:
Server Action
TheloginUser server action (client/app/actions/auth.ts) handles authentication:
Registration Flow
Horse Trust implements a two-step registration process:Step 1: Basic Account Creation
Users provide their basic information:- Full name
- Email address
- Phone number (international format)
- Password (minimum 8 characters)
Step 2: Identity Verification (Optional)
Sellers can verify their identity to gain trust badges:- Document number (DNI/Passport)
- Selfie holding ID document
Users can skip verification and still access the marketplace, but verified sellers receive priority placement and trust badges.
Password Comparison
The User model includes an instance method for secure password verification:Security Features
Sensitive Data Protection
Sensitive fields are automatically removed from JSON responses:Token Storage
1
Server-side Cookies
JWT tokens are stored in httpOnly cookies to prevent XSS attacks
2
Client-side Storage
Tokens are also stored in localStorage for client-side API calls
3
7-Day Expiration
Tokens automatically expire after 7 days for security
Protected Routes
The dashboard uses server-side authentication checking:Best Practices
Use Strong Passwords
Use Strong Passwords
The system requires minimum 8 characters. Encourage users to use passwords with mixed case, numbers, and symbols.
Enable HTTPS in Production
Enable HTTPS in Production
The secure cookie flag is automatically enabled in production to ensure tokens are only sent over HTTPS.
Implement Rate Limiting
Implement Rate Limiting
Add rate limiting to login endpoints to prevent brute force attacks.
Regular Token Rotation
Regular Token Rotation
Consider implementing refresh tokens for enhanced security in long-lived sessions.
Error Handling
The authentication system provides user-friendly error messages:Next Steps
Seller Verification
Learn how sellers verify their identity
Create Horse Listings
Start publishing horse listings

