This document describes the authentication system implemented for the MbetterClient API endpoints to ensure that logged-in users (admin/cashier) can access the API endpoints securely.
## Authentication System
The system uses a multi-layered authentication approach:
### 1. JWT (JSON Web Tokens)
- Short-lived access tokens for web interface authentication
- Generated via `/auth/token` endpoint with username/password
- Used for API requests with `Authorization: Bearer <token>` header
### 2. API Tokens
- Long-lived tokens for programmatic access
- Created and managed via the web dashboard
- Used for API requests with `Authorization: Bearer <api_token>` header
### 3. Role-Based Access Control
-**Admin**: Full access to all endpoints
-**Cashier**: Access to cashier-specific endpoints
-**Normal User**: Limited access to user-specific endpoints
### 4. Localhost Access
- Requests from `127.0.0.1` or `localhost` are automatically authenticated as admin
- Useful for development and testing
## Authentication Decorators
The system provides several authentication decorators:
### `@get_api_auth_decorator()`
- Basic authentication decorator
- Requires valid JWT or API token
- Used for most authenticated endpoints
### `@get_api_auth_decorator(require_admin=True)`
- Admin-only authentication decorator
- Requires valid authentication AND admin role
- Used for sensitive administrative endpoints
### `@require_role(allowed_roles)`
- Role-based access control decorator
- Requires specific roles for access
- Used for role-specific endpoints
## API Endpoints Authentication Requirements
### Public Endpoints (No Authentication)
These endpoints are accessible without authentication:
-`/auth/login` - Login page
-`/auth/token` - JWT token generation
-`/auth/logout` - Logout
-`/` - Dashboard home (redirects based on role)
### Authenticated Endpoints
These endpoints require valid authentication (JWT or API token):
- Role-based access control is implemented correctly
- Admin-only endpoints are properly protected
- Localhost access is maintained for development
- JWT and API token authentication works for all endpoints
- Comprehensive error handling is in place
The system provides a secure foundation for the MbetterClient application while maintaining flexibility for different user roles and access requirements.