Overview
Maxw AI uses Better-Auth for authentication and session management. Better-Auth provides a secure, flexible authentication system with support for email/password and OAuth providers.Authentication Flow
1. Sign Up
Users create accounts via email and password:2. Sign In
Authenticate with existing credentials:3. Session Management
Better-Auth automatically creates session cookies with these attributes:Auth API Endpoints
Better-Auth exposes multiple HTTP methods at/api/auth/[...all]:
Core Endpoints
Create a new user account with email and passwordRequest Body:
Authenticate with email and passwordRequest Body:
Invalidate current session and clear cookies
Retrieve current session dataResponse:
Update user profile information
Change user password (requires current password)
Delete user account and all associated data
Making Authenticated Requests
Server-Side (Next.js)
Use theauth.api.getSession() method to verify authentication:
Client-Side (React)
The Better-Auth client automatically manages session cookies:Database Schema
Better-Auth uses these tables in PostgreSQL:User Table
Core user accounts
Primary key, auto-generated unique identifier
User’s display name
Unique email address
Whether email has been verified
Profile image URL (optional)
User preferences and configuration (Canvas API tokens, etc.)
Account creation timestamp
Last update timestamp
Session Table
Active user sessions
Primary key
Unique session token (stored in HTTP-only cookie)
Foreign key to user table
Session expiration time
IP address of session creation
Browser/client user agent string
Session creation timestamp
Last activity timestamp
Account Table
OAuth provider credentials (for future OAuth integration)
Primary key
Provider-specific account ID
OAuth provider (e.g., “google”, “github”)
Foreign key to user table
OAuth access token
OAuth refresh token
Access token expiration
OAuth permission scopes
Verification Table
Security Best Practices
Session Security
Sessions automatically expire based on the
expiresAt timestamp. Better-Auth handles token rotation and invalidation.httpOnly: true- Prevents XSS attackssecure: true- Requires HTTPS (production)sameSite: "none"- Required for mobile app (Expo)
Password Requirements
Implement password validation on the client side:Rate Limiting
Error Responses
Authentication Errors
Mobile App (Expo)
The Better-Auth Expo plugin enables authentication in the React Native app:expo() plugin handles session persistence in React Native’s secure storage.
Environment Variables
Required environment variables for authentication: Configure in your.env file:
Next Steps
Chat API
Make authenticated requests to the AI chat endpoint
User Schema
Learn about user data models and settings
Better-Auth Docs
Explore Better-Auth documentation for advanced features