Authentication Overview
Mantis uses a custom email-based authentication system with session management powered bydjango-user-sessions. Authentication is required for all API endpoints.
Authentication Backend
The API uses a custom authentication backend (EmailBackEndAuth) that authenticates users using their email address instead of a traditional username.
Key Features
- Email as Username: Users authenticate with their email address
- Session-based Authentication: Secure session management using
django-user-sessions - Custom User Model:
CustomUserModelfrom theaccountsapp - Password Security: Django’s built-in password validation and hashing
User Model
The authentication system uses a custom user model with the following characteristics:User Fields
email- Primary identifier (replaces username)first_name- User’s first namelast_name- User’s last namepicture- Profile picture (optional)role- User role:ADMINISTRATIVOorTECNICOis_confirmed_mail- Email confirmation statussiganture_name- Signer name for documentssiganture_role- Signer role for documentsnotes- Additional user notes
User Roles
- ADMINISTRATIVO - Administrative users with full access
- TECNICO - Technical users with limited access
Authentication Flow
1. Login
To authenticate, send a POST request to the login endpoint:Although the field is named
username for compatibility, you must provide the user’s email address.2. Session Management
Upon successful authentication:- A session is created and stored in the database
- A session cookie (
sessionid) is returned - The session is managed by
django-user-sessions
3. Making Authenticated Requests
Include the session cookie in subsequent API requests:4. Logout
To end the session:Authentication Backend Implementation
The custom authentication backend is defined in: Location:source/app/src/common/EmailBackEndAuth.py
CORS Configuration
The API supports cross-origin requests with the following settings:Trusted Origins (CSRF)
For CSRF protection, the following origins are trusted:https://mantis.peisol.com.ec(Production)http://localhost:8000(Development)http://127.0.0.1:8000(Development)http://localhost:3000(Frontend dev)http://localhost:5173(Vite dev server)
Security Considerations
Password Validation
The API enforces Django’s default password validators:UserAttributeSimilarityValidator- Prevents passwords similar to user attributesMinimumLengthValidator- Enforces minimum password lengthCommonPasswordValidator- Prevents commonly used passwordsNumericPasswordValidator- Prevents numeric-only passwords
CSRF Protection
Session Security
- Sessions are stored in the database for better security and tracking
- Session cookies are managed automatically by Django
- Multiple active sessions per user are supported
Example: Python Client
Here’s an example of authenticating and making API requests using Python:Example: JavaScript Client
Here’s an example using JavaScript with the Fetch API:Troubleshooting
Login Fails with Valid Credentials
- Verify the email address is correct and registered
- Ensure the user account is active (
is_active=True) - Check that the password meets validation requirements
Session Cookie Not Persisted
- Ensure
credentials: 'include'is set in fetch requests - Check CORS configuration allows credentials
- Verify the session cookie domain matches your request origin
401 Unauthorized Responses
- Verify you’re logged in and the session is active
- Check that the session cookie is being sent with requests
- Session may have expired - try logging in again
Next Steps
API Overview
Explore available API endpoints
Projects API
Learn about project management endpoints