Skip to content

yupcoding1/blaize_backend

Repository files navigation

📚 Django + Supabase Backend Assessment

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.


📌 Table of Contents

  1. Project Overview
  2. Core Features
  3. Tech Stack
  4. Local Development Setup
  5. Database Schema Setup
  6. Project Documentation
  7. Contributing
  8. License
  9. Acknowledgments

Project Overview

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.


Core Features

  • 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 Supabase profiles table 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 the main branch directly to a production VM.


Tech Stack

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

Local Development Setup

Follow these steps to set up the project locally.

Prerequisites

  • Python 3.10+
  • pip and venv (included with Python)
  • A Supabase project (create one at https://supabase.com)

Step 1: Clone the Repository

git clone <YOUR_REPOSITORY_URL>
cd <your-project-directory>

Step 2: Create and Activate Virtual Environment

# Create the environment
python3 -m venv venv

# Activate (macOS/Linux)
source venv/bin/activate

# Activate (Windows - PowerShell)
.\venv\Scripts\Activate.ps1

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Configure Environment Variables

Create 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.

Step 5: Set Up the Supabase Database

  1. Go to your Supabase dashboard.
  2. Navigate to SQL Editor.
  3. Click New Query.
  4. Paste the SQL from the Database Schema Setup section below and click RUN.

Step 6: Run Django Migrations

python manage.py migrate

This sets up Django’s internal models (e.g., sessions, admin). It does not modify Supabase-managed tables.

Step 7: Start the Development Server

python manage.py runserver

Your API will be available at http://127.0.0.1:8000/.


Database Schema Setup

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.


Project Documentation

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.

Contributing

Contributions are welcome. Suggested workflow:

  1. Fork the repository.
  2. Create a feature branch (e.g., feature/otp-improvement).
  3. Write tests and update documentation.
  4. Open a pull request with a clear description and rationale.

Please follow existing code style, keep changes focused, and write descriptive commit messages.


Acknowledgments

  • Supabase for their powerful open-source backend platform.
  • Django for a robust web framework.
  • GitHub Actions for seamless CI/CD integration.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages