Supabase Integration

How we integrated Supabase authentication and database features


Overview

This project now uses Supabase for authentication instead of NextAuth.js. We've maintained backward compatibility with existing Prisma/MySQL database while adding Supabase's real-time features.

Key Changes

  • Replaced NextAuth.js with Supabase Auth
  • Added Supabase client and admin SDKs
  • Updated login and signup flows
  • Synced user data between Supabase and MySQL
  • Integrated Stripe webhook updates with Supabase

Authentication Flow

  1. User signs up or logs in through Supabase Auth
  2. User is redirected to the download page after authentication
  3. User data is automatically synced between Supabase and MySQL
  4. Session management uses Supabase session

Payment Integration

When a user makes a payment through Stripe, the webhook now updates both the MySQL database (for compatibility) and the Supabase database (for real-time features).

Database Schema

The Supabase database includes the following tables:

Users Table

create table users (
  id uuid references auth.users on delete cascade not null primary key,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
  updated_at timestamp with time zone default timezone('utc'::text, now()) not null,
  name text,
  email text unique,
  image text,
  stripe_subscription_id text,
  stripe_customer_id text,
  stripe_price_id text,
  stripe_current_period_end timestamp with time zone,
  downloaded_at timestamp with time zone,
  download_platform text
);

Posts Table

create table posts (
  id uuid default uuid_generate_v4() primary key,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
  updated_at timestamp with time zone default timezone('utc'::text, now()) not null,
  title text,
  content jsonb,
  published boolean default false,
  author_id uuid references users(id) on delete cascade not null
);

Accounts Table

create table accounts (
  id uuid default uuid_generate_v4() primary key,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
  updated_at timestamp with time zone default timezone('utc'::text, now()) not null,
  user_id uuid references users(id) on delete cascade not null,
  type text not null,
  provider text not null,
  provider_account_id text not null,
  refresh_token text,
  access_token text,
  expires_at bigint,
  token_type text,
  scope text,
  id_token text,
  session_state text
);

Sessions Table

create table sessions (
  id uuid default uuid_generate_v4() primary key,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
  updated_at timestamp with time zone default timezone('utc'::text, now()) not null,
  session_token text not null unique,
  user_id uuid references users(id) on delete cascade not null,
  expires timestamp with time zone not null
);

Verification Tokens Table

create table verification_tokens (
  id uuid default uuid_generate_v4() primary key,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null,
  updated_at timestamp with time zone default timezone('utc'::text, now()) not null,
  identifier text not null,
  token text not null unique,
  expires timestamp with time zone not null
);

File Structure

  • lib/supabase/client.ts - Supabase client configuration
  • lib/supabase/auth.ts - Authentication utilities
  • lib/supabase/admin.ts - Admin client for server-side operations
  • supabase/migrations/ - Database migration scripts

Environment Variables

Make sure to set the following environment variables:

NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key

Setting Up the Database

  1. Install the Supabase CLI: npm install -g supabase
  2. Start the Supabase local development stack: supabase start
  3. Apply migrations: supabase db push