Introduction
The Inmobiliaria API uses Better Auth as its authentication framework, providing a secure, flexible, and developer-friendly authentication system. Better Auth handles session management, OAuth providers, email verification, and password reset flows.Email & Password
Traditional authentication with email verification
OAuth (Google)
Sign in with Google OAuth 2.0
Session Management
Understanding sessions and authentication state
API Authentication
Protecting routes with authentication middleware
Authentication Methods
The API supports two primary authentication methods:1. Email and Password
Traditional authentication where users register with an email address and password. This method includes:- Email verification (required before full access)
- Password reset via email
- Secure password hashing
2. OAuth (Google)
Social authentication using Google OAuth 2.0. Users can sign in with their Google account without creating a separate password.Better Auth Configuration
The authentication system is configured insrc/auth/index.ts:
Database Schema
Better Auth uses four main tables to manage authentication:Users Table
Sessions Table
Stores active user sessions:Accounts Table
Stores OAuth provider accounts and password hashes:Verification Tokens Table
Stores email verification and password reset tokens:Security Features
The authentication system includes several security features:1. HTTP-Only Cookies
Sessions are stored in HTTP-only cookies to prevent XSS attacks:2. CORS Configuration
Cross-Origin Resource Sharing is configured to allow requests from the frontend:3. Trusted Origins
Only specified origins can make authenticated requests:4. Password Hashing
Passwords are automatically hashed using bcrypt before storage. Better Auth handles this internally.5. Email Verification
Users must verify their email address before gaining full access to the system. Verification tokens expire after 1 hour.User Roles
The system supports two user roles:User
User
Standard user role with access to:
- View properties
- Submit contact inquiries
- Manage their own profile
Admin
Admin
Administrative role with full access to:
- All user capabilities
- Create, update, and delete properties
- Manage users
- View analytics and statistics
- Access admin-only endpoints
Environment Variables
Required environment variables for authentication:API Endpoints
All Better Auth endpoints are available under/api/auth/*:
| Endpoint | Method | Description |
|---|---|---|
/api/auth/sign-up/email | POST | Register with email and password |
/api/auth/sign-in/email | POST | Sign in with email and password |
/api/auth/sign-in/google | GET | Initiate Google OAuth flow |
/api/auth/sign-out | POST | Sign out and invalidate session |
/api/auth/verify-email | GET | Verify email address |
/api/auth/forget-password | POST | Request password reset |
/api/auth/reset-password | POST | Reset password with token |
/api/session | GET | Get current session information |
All Better Auth endpoints are automatically handled by the framework. See individual guides for detailed request/response formats.
Next Steps
Email/Password Auth
Implement email and password authentication
OAuth Integration
Set up Google OAuth authentication
Session Management
Learn about session handling
Protect Routes
Use authentication middleware