Hikari

Development in Production for Supabase

Develop locally

If you haven't already done so, clone your Github repository to your local machine.

Configure Auth

Follow this guide to set up an OAuth app with GitHub and configure Supabase to use it as an auth provider.

In your Supabase project, navigate to auth > URL configuration and set your main production URL (e.g. https://your-deployment-url.vercel.app) as the site url.

Next, in your Vercel deployment settings, add a new Production environment variable called NEXT_PUBLIC_SITE_URL and set it to the same URL. Make sure to deselect preview and development environments to make sure that preview branches and local development work correctly.

Install dependencies

Ensure you have pnpm installed and run:

pnpm install

Local Development with Supabase

Prerequisites

Install Docker:

  • Download and install Docker from here.

Setup Environment Files:

  • Copy or rename .env.local.example to .env.local.
  • Copy or rename .env.example to .env.

Starting Local Supabase Instance

Start Supabase:

  • Open your terminal.
  • Run the following command to start a local Supabase instance and set up the database schema:
pnpm supabase:start 
  • Note the URLs provided in the terminal output for accessing different services within the Supabase stack.

Configure Environment Variables:

  • Copy the value of service_role_key from the terminal output.
  • Open your .env.local file.
  • Set SUPABASE_SERVICE_ROLE_KEY in your .env.local file with the copied value.

Print Supabase URLs:

  • Open your terminal.
  • Run the following command to print out the URLs:
pnpm supabase:status 
  • This will print the following keys that we will use in our environment variables. Copy these values and paste them in your .env.local file:
.env.local
API URL: http://127.0.0.1:54321 # NEXT_PUBLIC_SUPABASE_URL 
anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ******xad # NEXT_PUBLIC_SUPABASE_ANON_KEY 
service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9*******sad # SUPABASE_SERVICE_ROLE_KEY 

Link Local Supabase Instance to Project:

  • Open your terminal.
  • Run the following command:
pnpm supabase:link 
  • Navigate to the Supabase project you created.
  • Enter your database password when prompted.

Database Password Reset

Reset Database Password:

  • Visit your database settings.
  • Click "Reset database password".
  • Copy the new password and store it in a password manager.

Schema Changes and Data Seeding

Pull Schema Changes:

  • Open your terminal.
  • Run the following command to pull schema changes from your remote database:
pnpm supabase:pull 

Seed Local Database:

  • Open your terminal.
  • Run the following command to generate seed data:
pnpm supabase:generate-seed 
pnpm supabase:reset 

Generating Types and Migrations

Generate TypeScript Types:

  • Open your terminal.
  • Run the following command to generate TypeScript types to match your schema:
pnpm supabase:generate-types 

Generate Migration File:

  • Open your terminal.
  • Run the following command to automatically generate a migration file with all the changes you've made to your local database schema:
pnpm supabase:generate-migration 
  • Push those changes to your remote database with:
pnpm supabase:push 

Use the Stripe CLI to Test Webhooks

Setting Up Stripe CLI

Login to Stripe Account:

pnpm stripe:login 
  • This will print a URL to navigate to in your browser and provide access to your Stripe account.

Start Local Webhook Forwarding:

  • Run the following command:
pnpm stripe:listen 
  • This will print a webhook secret (such as whsec_***) to the console.
  • Set STRIPE_WEBHOOK_SECRET to this value in your .env.local file.
  • If you haven't already, set NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY and STRIPE_SECRET_KEY in your .env.local file using the test mode keys from your Stripe dashboard.

Final environment variables

By now your environment variables should look like this:

.env
SUPABASE_AUTH_EXTERNAL_GITHUB_REDIRECT_URI="http://127.0.0.1:54321/auth/v1/callback"
SUPABASE_AUTH_EXTERNAL_GITHUB_CLIENT_ID="Ov23li********Q"
SUPABASE_AUTH_EXTERNAL_GITHUB_SECRET="96e4**************************34d"
.env.local
NEXT_PUBLIC_APP_URL="http://localhost:3000"
 
# These environment variables are used for Supabase Local Dev
NEXT_PUBLIC_SUPABASE_ANON_KEY="eyJhbGciOiJIUzI1NiIsInR************************************WNReilDMblYTn_I0"
NEXT_PUBLIC_SUPABASE_URL="http://127.0.0.1:54321"
SUPABASE_SERVICE_ROLE_KEY="eyJhbGciOiJIUzI1Ni******************************************Zx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU"
 
# Get these from Stripe dashboard
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY="pk_test_51PXT**********************************PpPple1"
STRIPE_SECRET_KEY="sk_test_5********************************************I7h"
STRIPE_WEBHOOK_SECRET="whsec_483**********************************d2118"
 
# Optional just to keep your database safe! :D
DB_PASSWORD="W23**********s8e"

Run the Next.js Client

Start Development Server:

  • Open a separate terminal.
  • Run the following command to start the development server:
pnpm dev 
  • Note that webhook forwarding and the development server must be running concurrently in two separate terminals for the application to work correctly.

View Application:

Going live

Archive testing products

Archive all test mode Stripe products before going live. Before creating your live mode products, make sure to follow the steps below to set up your live mode env vars and webhooks.

Configure production environment variables

To run the project in live mode and process payments with Stripe, switch Stripe from "test mode" to "production mode." Your Stripe API keys will be different in production mode, and you will have to create a separate production mode webhook. Copy these values and paste them into Vercel, replacing the test mode values.

On this page