Spaces:
Running
Running
File size: 3,047 Bytes
ba5edb0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# Admin Authentication Setup
This document explains how to set up admin authentication for the PromptAid Vision application.
## Environment Variables
Add these environment variables to your `.env` file or Hugging Face Space secrets:
### Required Variables
```bash
# Admin password for authentication
ADMIN_PASSWORD=your-secure-admin-password-here
# JWT secret key for token signing (use a strong, random key)
JWT_SECRET_KEY=your-secure-jwt-secret-key-here
```
### Optional Variables
```bash
# Database connection
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
# Storage configuration
STORAGE_PROVIDER=local
STORAGE_DIR=./uploads
```
## How It Works
### 1. Admin Login
- Users click "Admin Login" in the header navigation
- They enter the admin password
- If correct, they receive a JWT token valid for 24 hours
### 2. Authentication Flow
- Frontend stores the JWT token in localStorage
- Token is sent with each admin API request in Authorization header
- Backend verifies token validity and role
### 3. Security Features
- JWT tokens expire after 24 hours
- Tokens are verified on each admin request
- Password is stored in environment variables (not in code)
## API Endpoints
### POST `/api/admin/login`
- **Purpose**: Authenticate admin user
- **Body**: `{"password": "admin_password"}`
- **Response**: `{"token": "jwt_token", "expires_at": "timestamp"}`
### POST `/api/admin/verify`
- **Purpose**: Verify admin token
- **Headers**: `Authorization: Bearer <token>`
- **Response**: `{"valid": true/false, "message": "..."}`
### GET `/api/admin/status`
- **Purpose**: Get admin status (protected endpoint)
- **Headers**: `Authorization: Bearer <token>`
- **Response**: `{"status": "authenticated", "role": "admin", "timestamp": "..."}`
## Development vs Production
### Development
- Default password: `admin123`
- Default JWT secret: `your-secret-key-change-in-production`
- **⚠️ Change these in production!**
### Production
- Use strong, random passwords
- Use secure JWT secret keys
- Store secrets in environment variables or Hugging Face Space secrets
- Consider implementing password hashing for additional security
## Future Enhancements
- User-specific accounts and permissions
- Role-based access control
- Password hashing with bcrypt
- Session management
- Audit logging
- Two-factor authentication
## Troubleshooting
### Common Issues
1. **"Invalid admin password"**
- Check that `ADMIN_PASSWORD` environment variable is set correctly
- Ensure no extra spaces or characters
2. **"Token is invalid or expired"**
- Token may have expired (24-hour limit)
- Try logging in again
- Check `JWT_SECRET_KEY` is consistent
3. **"Method Not Allowed"**
- Ensure admin router is properly included in main.py
- Check API endpoint URLs are correct
### Debug Steps
1. Verify environment variables are loaded
2. Check backend logs for authentication errors
3. Verify JWT token format in browser localStorage
4. Test API endpoints directly with tools like curl or Postman
|