User Management
This guide covers user registration, authentication flows, and user data management in the BE Monorepo.User Schema
The user table is defined insrc/db/schema.ts:
User Fields
- id: Unique text identifier (primary key)
- name: User’s display name (required)
- email: User’s email address (unique, required)
- emailVerified: Email verification status (default: false)
- image: Optional profile image URL
- timestamps: Automatic created/updated/deleted timestamps
Account Table
The account table manages authentication providers and credentials:User Registration
Better Auth handles user registration through its built-in API endpoints. Users can register via:Email and Password
Clients can register by sending a POST request to/api/auth/sign-up/email:
Response
User Login
Authenticate existing users through the Better Auth API:Authentication Flow
- Client sends credentials to
/api/auth/sign-in/email - Better Auth validates credentials against hashed password in account table
- On success, creates a new session with token
- Returns user data and session token
- Client stores session token (typically in cookies)
Password Handling
Better Auth automatically handles password security:- Hashing: Passwords are hashed using bcrypt before storage
- Validation: Password strength can be configured
- Storage: Hashed passwords stored in
account.passwordfield - Comparison: Secure comparison during authentication
Password Security
User Roles and Permissions
The current implementation uses a simple user model without built-in roles. To add role-based access control:Extend User Schema
Role-Based Middleware
Verification Table
Email verification tokens are managed in a separate table:User Operations
Get Current User
Update User Profile
Delete User
API Endpoints
Better Auth provides these endpoints automatically:POST /api/auth/sign-up/email- Register new userPOST /api/auth/sign-in/email- Login userPOST /api/auth/sign-out- Logout userGET /api/auth/session- Get current sessionPOST /api/auth/verify-email- Verify emailPOST /api/auth/reset-password- Reset password
/api/auth/docs.
Next Steps
- Session Handling - Manage user sessions
- Authentication Setup - Configure Better Auth
