Skip to main content

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 in server/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

The loginUser server action (client/app/actions/auth.ts) handles authentication:
Tokens are stored both in httpOnly cookies (server-side) and localStorage (client-side) for different use cases.

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

The system requires minimum 8 characters. Encourage users to use passwords with mixed case, numbers, and symbols.
The secure cookie flag is automatically enabled in production to ensure tokens are only sent over HTTPS.
Add rate limiting to login endpoints to prevent brute force attacks.
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