Overview
The Fataplus API uses session-based authentication with Cloudflare D1 for secure, scalable authentication. The backend is configured with authentication enabled globally across all endpoints.Authentication Methods
The API supports the following authentication methods:1. Session-Based Authentication
The primary authentication method using HTTP-only cookies for secure session management.Authentication is enabled globally across the API (configured in
config.ts:10).Sessions are stored in Cloudflare D1 database for persistence and scalability (configured in
config.ts:5).How It Works
- User authenticates via the
/auth/loginendpoint - Server creates a session in D1 database
- Session ID is returned as an HTTP-only cookie
- Subsequent requests include the session cookie automatically
- Server validates session on each request
2. API Token Authentication
For server-to-server communication or when cookies are not available.API tokens can be generated through the Admin UI under User Settings.
Role-Based Access Control
The API implements role-based permissions at the collection level:Available Roles
Full access to all collections and operations. Can manage clients, projects, and agricultural data.
Limited access based on tenant isolation. Can read their own data and manage their projects.
Collection Permissions
Clients Collection
- Read: Both admins and clients can view client records
- Write: Only admins can create/update/delete clients
Projects Collection
- Read: Both admins and clients can view projects
- Write: Both admins and clients can create/update projects
Agricultural Data Collection
- Read: Both admins and clients can view agricultural data
- Write: Both admins and clients can create/update agricultural data
CORS Configuration
The API is configured with CORS support for cross-origin requests:Allowed Origins
Cookies and authentication headers are allowed in cross-origin requests.
Making Cross-Origin Requests
OPTIONS) requests:
MCP Endpoint Authentication
The Model Context Protocol endpoint has relaxed CORS for AI tool compatibility:The MCP endpoint at
/api/system/mcp allows all origins (*) to support AI agents and development tools.Authentication Flow
Standard Web Application Flow
API Token Flow
Security Best Practices
Recommendations
- Use HTTPS: All production requests must use HTTPS
- Session Cookies: Let the browser handle session cookies automatically
- CORS Origins: Only allowed origins can make authenticated requests
- Token Storage: Store API tokens securely in environment variables
- Password Security: Uses bcryptjs for password hashing (see
package.json:14)
Error Responses
401 Unauthorized
Returned when authentication is missing or invalid:403 Forbidden
Returned when authenticated but lacking required permissions:Testing Authentication
Use the Admin UI to test authentication flows:- User creation and management
- Role assignment
- API token generation
- Permission testing
Next Steps
API Endpoints
Explore authenticated endpoint usage
Multi-Tenancy
Learn about tenant isolation
MCP Protocol
Integrate AI agents with authentication
Admin UI
Manage users and permissions