This repository contains a scalable Django backend application integrated with Supabase for authentication and database management. It is designed to fulfill assessment requirements and is production-ready with an automated CI/CD deployment to a cloud VM.
- Project Overview
- Core Features
- Tech Stack
- Local Development Setup
- Database Schema Setup
- Project Documentation
- Contributing
- License
- Acknowledgments
This Django backend project leverages Supabase for authentication and PostgreSQL database management. It provides a secure and extensible API for user registration, login, and profile management. The backend is optimized for production deployment on a Google Cloud VM and includes a GitHub Actions workflow for continuous integration and deployment.
-
Supabase Authentication Integration
Full support for user login and signup using Supabase Auth. -
Multiple Authentication Methods
- Email + Password
- Email + OTP (One-Time Password)
- Phone + OTP
- Google Sign-In (OAuth)
-
Persistent User Profiles
Automatically creates and maintains user profiles in a Supabaseprofilestable upon registration. -
Custom Session Management
Implements a custom Django authentication backend with a strict 24-hour session expiry policy. -
Protected API Endpoints
/auth/profile: Retrieve authenticated user profile/auth/edit-profile: Update profile details
Accessible only with a valid, non-expired session.
-
Automated Deployment
GitHub Actions workflow deploys all changes merged to themainbranch directly to a production VM.
| Category | Technology | Purpose |
|---|---|---|
| Backend | Django, Django REST Framework | Core application logic and API framework |
| Database | Supabase (PostgreSQL) | Primary data storage and user profile management |
| Authentication | Supabase Auth | Handles user authentication flows |
| Runtime | Gunicorn | WSGI server for running the Django app |
| Web Server | Nginx | Reverse proxy and static file serving |
| Process Manager | Supervisor | Ensures Gunicorn stays running |
| Deployment | Google Cloud Platform (GCP) | VM (Compute Engine) for hosting the application |
| CI/CD | GitHub Actions | Automated testing and deployment pipeline |
Follow these steps to set up the project locally.
- Python 3.10+
- pip and venv (included with Python)
- A Supabase project (create one at https://supabase.com)
git clone <YOUR_REPOSITORY_URL>
cd <your-project-directory># Create the environment
python3 -m venv venv
# Activate (macOS/Linux)
source venv/bin/activate
# Activate (Windows - PowerShell)
.\venv\Scripts\Activate.ps1pip install -r requirements.txtCreate a .env file in the root directory and add the following:
# ----------------------------------------------
# DJANGO CORE SETTINGS
# ----------------------------------------------
SECRET_KEY="your-django-secret-key-goes-here"
DEBUG=True
ALLOWED_HOSTS=127.0.0.1,localhost
# ----------------------------------------------
# SUPABASE CREDENTIALS
# ----------------------------------------------
SUPABASE_URL="https://<your-project-ref>.supabase.co"
SUPABASE_KEY="your-supabase-anon-key"
SUPABASE_SERVICE_KEY="your-supabase-service-role-key"Ensure .env is listed in .gitignore to avoid committing sensitive data.
- Go to your Supabase dashboard.
- Navigate to SQL Editor.
- Click New Query.
- Paste the SQL from the Database Schema Setup section below and click RUN.
python manage.py migrateThis sets up Django’s internal models (e.g., sessions, admin). It does not modify Supabase-managed tables.
python manage.py runserverYour API will be available at http://127.0.0.1:8000/.
Run the following SQL script in your Supabase SQL Editor:
-- 1. Create the 'profiles' table
CREATE TABLE public.profiles (
id UUID NOT NULL PRIMARY KEY,
username TEXT UNIQUE,
email TEXT UNIQUE,
full_name TEXT,
avatar_url TEXT,
updated_at TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT id_fkey FOREIGN KEY (id)
REFERENCES auth.users(id) ON DELETE CASCADE
);
-- 2. Enable Row Level Security
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
-- 3. Create RLS Policies
CREATE POLICY "Users can view their own profile."
ON public.profiles FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can update their own profile."
ON public.profiles FOR UPDATE
USING (auth.uid() = id)
WITH CHECK (auth.uid() = id);
-- 4. Trigger Function to auto-create profile
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.profiles (id, email, username)
VALUES (new.id, new.email, split_part(new.email, '@', 1));
RETURN new;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- 5. Trigger on auth.users
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE PROCEDURE public.handle_new_user();Run this once to create the profiles table, enable row-level security, add policies, and wire the trigger that auto-populates profiles on new user creation.
Additional documentation is available in the repository:
- API_DOCUMENTATION.md: Detailed API reference with endpoints, request/response schemas, and error codes.
- TESTING_GUIDE.md: Step-by-step guide for testing every API endpoint using curl.
- DEPLOYMENT_GUIDE.md: End-to-end guide for deploying this application to a GCP VM with an automated CI/CD pipeline.
Contributions are welcome. Suggested workflow:
- Fork the repository.
- Create a feature branch (e.g.,
feature/otp-improvement). - Write tests and update documentation.
- Open a pull request with a clear description and rationale.
Please follow existing code style, keep changes focused, and write descriptive commit messages.
- Supabase for their powerful open-source backend platform.
- Django for a robust web framework.
- GitHub Actions for seamless CI/CD integration.