Skip to main content

Technobits Server Django Package - Modular auth, payments, and utilities for Django

Project description

Technobits Django Authentication Backend Package

A complete Django backend package providing authentication and email services with minimal setup. Focused on secure user authentication, Google OAuth, and SendInBlue email integration.

🚀 Quick Installation

1. Install the Package

# Install from local development
pip install -e /path/to/Library/packages/server-django

# Or install from PyPI (when published)
pip install technobits-server-django

2. Add to Django Settings

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    # Required third-party apps
    'rest_framework',
    'rest_framework_simplejwt',
    'corsheaders',
    
    # Technobits modules
    'technobits.auth',
    'technobits.email',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# REST Framework Configuration
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}

# JWT Configuration
from datetime import timedelta
SIMPLE_JWT = {
    'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
    'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
    'ROTATE_REFRESH_TOKENS': True,
    'BLACKLIST_AFTER_ROTATION': True,
}

# CORS Configuration
CORS_ALLOWED_ORIGINS = [
    "http://localhost:3000",  # Your frontend URL
    "http://127.0.0.1:3000",
]

CORS_ALLOW_CREDENTIALS = True

# Technobits Configuration
TECHNOBITS_CONFIG = {
    'AUTH': {
        'GOOGLE_OAUTH_CLIENT_ID': os.getenv('GOOGLE_OAUTH_CLIENT_ID', ''),
        'ENABLE_GOOGLE_LOGIN': True,
        'ENABLE_EMAIL_VERIFICATION': True,
        'JWT_EXPIRY_HOURS': 24,
    },
    'EMAIL': {
        'FROM_EMAIL': os.getenv('FROM_EMAIL', 'noreply@example.com'),
        'SMTP_HOST': os.getenv('EMAIL_HOST', 'smtp.gmail.com'),
        'SMTP_PORT': int(os.getenv('EMAIL_PORT', '587')),
        'SMTP_USER': os.getenv('EMAIL_HOST_USER', ''),
        'SMTP_PASSWORD': os.getenv('EMAIL_HOST_PASSWORD', ''),
        'USE_TLS': True,
    }
}

3. Add URLs

# urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    
    # Technobits API endpoints
    path('api/auth/', include('technobits.auth.urls')),
    path('api/email/', include('technobits.email.urls')),
]

4. Run Migrations

python manage.py migrate

5. Create Environment File

Create a .env file in your project root:

# Django Settings
SECRET_KEY=your-secret-key-here
DEBUG=True

# Google OAuth
GOOGLE_OAUTH_CLIENT_ID=your-google-client-id

# reCAPTCHA (optional)
RECAPTCHA_SECRET_KEY=your-recaptcha-secret-key

# Email
FROM_EMAIL=noreply@yoursite.com
# SendInBlue API (recommended for production)
SENDINBLUE_API_KEY=your-sendinblue-api-key

# Or use SMTP
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_HOST_USER=your-email@gmail.com
EMAIL_HOST_PASSWORD=your-email-password

🎯 Available API Endpoints

Authentication

  • POST /api/auth/register/ - User registration
  • POST /api/auth/login/ - User login
  • POST /api/auth/google/ - Google OAuth login
  • POST /api/auth/logout/ - User logout
  • POST /api/auth/refresh/ - Refresh JWT token
  • GET /api/auth/me/ - Get current user
  • POST /api/auth/forgot-password/ - Request password reset
  • POST /api/auth/reset-password/ - Reset password
  • POST /api/auth/change-password/ - Change password
  • GET /api/auth/health/ - Health check

Email Services

  • POST /api/email/send/ - Send plain email
  • POST /api/email/send-template/ - Send template email using SendInBlue

📝 Usage Examples

Registration API Call

import requests

response = requests.post('http://localhost:8000/api/auth/register/', {
    'email': 'user@example.com',
    'password': 'securepassword123',
    'name': 'John Doe'
})

print(response.json())
# Output: {"user": {"id": 1, "email": "user@example.com", "name": "John Doe"}, "message": "User registered successfully"}

Login API Call

response = requests.post('http://localhost:8000/api/auth/login/', {
    'email': 'user@example.com',
    'password': 'securepassword123'
})

data = response.json()
access_token = data['access']
refresh_token = data['refresh']

Send Email with Template

response = requests.post('http://localhost:8000/api/email/send-template/', {
    'to': 'user@example.com',
    'template_id': 1,  # Password reset template
    'params': {
        'name': 'John Doe',
        'reset_url': 'https://yoursite.com/reset-password?token=abc123'
    }
}, headers={
    'Authorization': f'Bearer {access_token}'
})

email_response = response.json()

Forgot Password Flow

# Step 1: Request password reset
response = requests.post('http://localhost:8000/api/auth/forgot-password/', {
    'email': 'user@example.com'
})

# This sends an email with reset instructions
print(response.json())
# Output: {"message": "Password reset instructions sent to your email"}

🔧 Advanced Configuration

Custom User Model

# settings.py
AUTH_USER_MODEL = 'auth.User'  # Uses Django's default User model

# Or extend with custom fields
TECHNOBITS_CONFIG = {
    'AUTH': {
        'USER_MODEL': 'myapp.CustomUser',
        'ENABLE_EMAIL_VERIFICATION': True,
        'ENABLE_GOOGLE_LOGIN': True,
    }
}

Email Templates

# Custom email templates
TECHNOBITS_CONFIG = {
    'EMAIL': {
        'TEMPLATES': {
            'PASSWORD_RESET': 'emails/password_reset.html',
            'EMAIL_VERIFICATION': 'emails/verify_email.html',
        }
    }
}

SendInBlue Integration

# settings.py
TECHNOBITS_CONFIG = {
    'EMAIL': {
        'USE_SENDINBLUE': True,
        'SENDINBLUE_API_KEY': os.getenv('SENDINBLUE_API_KEY'),
        'TEMPLATES': {
            'PASSWORD_RESET': 1,
            'EMAIL_VERIFICATION': 2,
            'WELCOME': 3,
        }
    }
}

🚀 Production Deployment

1. Environment Variables

# Production environment
DEBUG=False
ALLOWED_HOSTS=yoursite.com,www.yoursite.com
SECRET_KEY=your-production-secret-key

# Database
DATABASE_URL=postgresql://user:pass@localhost/dbname

# Email (SendInBlue recommended)
SENDINBLUE_API_KEY=your-production-sendinblue-api-key

# Google OAuth (Production)
GOOGLE_OAUTH_CLIENT_ID=your-production-google-client-id
GOOGLE_OAUTH_CLIENT_SECRET=your-production-google-client-secret

# reCAPTCHA (Production)
RECAPTCHA_SECRET_KEY=your-production-recaptcha-secret

2. Security Settings

# settings.py (production)
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

🔍 Testing

Run Tests

# Install test dependencies
pip install technobits-server-django[dev]

# Run tests
python manage.py test technobits

Test API Endpoints

# Health check
curl http://localhost:8000/api/auth/health/

# Register user
curl -X POST http://localhost:8000/api/auth/register/ \
  -H "Content-Type: application/json" \
  -d '{"email": "test@example.com", "password": "testpass123", "name": "Test User"}'

# Login
curl -X POST http://localhost:8000/api/auth/login/ \
  -H "Content-Type: application/json" \
  -d '{"email": "test@example.com", "password": "testpass123"}'

📦 Dependencies

The package automatically installs:

  • Django (>=4.0,<6.0) - Web framework
  • Django REST Framework (>=3.14.0) - API framework
  • Simple JWT (>=5.3.0) - JWT authentication
  • CORS Headers (>=4.3.0) - CORS support
  • Google Auth (>=2.23.0) - Google OAuth integration
  • Requests (>=2.31.0) - HTTP requests
  • Python Dotenv (>=1.0.0) - Environment variables
  • SendInBlue API (>=7.6.0) - Email service with templates

🤝 Support

  • Check the health endpoint: GET /api/auth/health/
  • Enable debug mode: DEBUG=True in settings
  • Check Django logs for detailed error messages
  • Verify environment variables are loaded correctly

📄 License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

technobits_server_django-1.0.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

technobits_server_django-1.0.0-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file technobits_server_django-1.0.0.tar.gz.

File metadata

  • Download URL: technobits_server_django-1.0.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for technobits_server_django-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d0418bfb0640111ad4a2b210bb294676842b5f61bc1b43bb59e6a348fb2bd349
MD5 465ae9a43635a731da453e9cf08f9ddf
BLAKE2b-256 5f32d8fd0458f72c621616c1464f53b14b075c11076e9724f1f8e067b668fedd

See more details on using hashes here.

File details

Details for the file technobits_server_django-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for technobits_server_django-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 643135d4eb6a9f457fe8b45d2069f0a08dc053a0706517018db68e78335cd58e
MD5 4ccffdc9b8694ef4fe8ad04ee5c84a8b
BLAKE2b-256 e6b662956fe9cc628eaa3d7128896a4c7d9d89ceb30cf9ce187f69ea18583e0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page