Skip to main content

Supabase Connection Setup Guide

Project: AI-Powered Photo Journaling iOS App Database: Supabase PostgreSQL Created: 2025-01-11 For: Backend and Frontend developers

Overview

This guide explains how to connect to the Supabase database from:
  • Backend (Next.js API routes)
  • Frontend (Flutter iOS app)

Supabase Project Information

Environment Variables Required

Create a .env.local file in your project root:

How to Get Credentials

  1. Go to Supabase Dashboard
  2. Select your project
  3. Navigate to SettingsAPI
  4. Copy:
    • Project URLNEXT_PUBLIC_SUPABASE_URL
    • anon public key → NEXT_PUBLIC_SUPABASE_ANON_KEY
    • service_role key → SUPABASE_SERVICE_ROLE_KEY (keep secret!)

Backend Setup (Next.js)

Install Supabase Client

Create Supabase Client

Create lib/supabase/server.ts:

Using in API Routes


Frontend Setup (Flutter)

Install Supabase Flutter Package

Add to pubspec.yaml:

Initialize Supabase

Create lib/core/supabase/supabase_config.dart:

Using in Flutter App

Initialize in main.dart:

Querying Data


Authentication

Sign Up

Sign In

Get Current User


Running Migrations

Option 1: Supabase CLI

Option 2: Supabase Dashboard

  1. Go to SQL Editor in Supabase Dashboard
  2. Copy/paste migration SQL files
  3. Run each migration in order (001, 002, 003, etc.)

Option 3: Programmatically (Backend)


Common Query Patterns

Get User’s Journal Entries (with Emotions)

Filter by Emotions

Upload Photo to Storage


Security Best Practices

1. Never Expose Service Role Key

  • DO NOT include service role key in frontend code
  • DO NOT commit service role key to version control
  • USE environment variables only
  • ROTATE keys if exposed

2. Use RLS Policies

All queries automatically respect Row Level Security policies:
  • Users can only access their own data
  • No need to manually filter by user_id in most queries
  • Policies are enforced at database level

3. Validate Input

Always validate user input before database operations:

Troubleshooting

Connection Issues

Error: Invalid API key
  • Fix: Verify NEXT_PUBLIC_SUPABASE_ANON_KEY is correct
  • Check Supabase Dashboard → Settings → API
Error: JWT expired
  • Fix: Refresh user’s auth token
  • Supabase client auto-refreshes tokens by default

RLS Policy Issues

Error: new row violates row-level security policy
  • Fix: Ensure user is authenticated (auth.uid() is not null)
  • Check that RLS policies allow the operation
Issue: Query returns no results
  • Cause: RLS policies filtering out data
  • Debug: Use service role key to bypass RLS (testing only!)

Migration Issues

Error: relation already exists
  • Cause: Migration already applied
  • Fix: Use IF NOT EXISTS in CREATE statements
  • Or track applied migrations in a separate table

Performance Tips

  1. Use indexes: All common queries are indexed (see schema)
  2. Limit results: Always use .limit() for list queries
  3. Select specific columns: Don’t use SELECT * in production
  4. Use connection pooling: Supabase handles this automatically
  5. Cache frequently accessed data: Implement app-level caching


Need help? Contact @dba or @backend-developer for database questions.