Supabase
Overview
Supabase is integrated into this React Native app as the backend-as-a-service platform, providing authentication, database (PostgreSQL), real-time subscriptions, and storage. It serves as the primary backend for user authentication, data persistence, and API interactions.
Configuration
Supabase is configured in lib/supabase.ts:
- URL: Set via
EXPO_PUBLIC_SUPABASE_URLenvironment variable - Anon Key: Set via
EXPO_PUBLIC_SUPABASE_ANON_KEYenvironment variable - Storage: Uses AsyncStorage for session persistence
- Auto Refresh: Token refresh is handled automatically
- Session Persistence: Sessions are persisted across app restarts
- URL Detection: Disabled (not needed for React Native)
Integration Details
Authentication
Authentication Methods
The app supports multiple authentication methods:
-
Magic Link (OTP): Email-based one-time password
- Component:
components/auth/withMagic.tsx - Flow: User enters email → receives OTP → verifies code
- Component:
-
Google Sign-In: OAuth with Google
- Component:
components/auth/withGoogle.tsx - Uses
@react-native-google-signin/google-signin - Exchanges Google ID token with Supabase
- Component:
-
Apple Sign-In: OAuth with Apple
- Component:
components/auth/withApple.tsx - Uses
expo-apple-authentication - Exchanges Apple ID token with Supabase
- Component:
-
Facebook Sign-In: OAuth with Facebook
- Component:
components/auth/withFacebook.tsx - Uses OAuth flow with URL redirects
- Component:
-
Twitter Sign-In: OAuth with Twitter
- Component:
components/auth/withTwitter.tsx - Uses OAuth flow with URL redirects
- Component:
Session Management
// Start auto-refresh (on app start)
supabase.auth.startAutoRefresh();
// Stop auto-refresh (on app background)
supabase.auth.stopAutoRefresh();
// Get current session
const {
data: { session },
} = await supabase.auth.getSession();
// Listen for auth state changes
supabase.auth.onAuthStateChange((event, session) => {
// Handle auth state changes
});
// Sign out
await supabase.auth.signOut();Database Schema
The app uses Prisma for database schema management with multi-schema support:
- Public Schema: Application tables (UserProfile, Image, Theme)
- Auth Schema: Supabase authentication tables (users, identities, etc.)
Linking Auth and Public Schema
User profiles in the public schema are linked to Supabase auth users via foreign key:
model UserProfile {
userId String @id @map("user_id") @db.Uuid
users users @relation(fields: [userId], references: [id])
// ... other fields
}Environment Setup
1. Create Supabase Project
- Go to Supabase Dashboard
- Create a new project
- Wait for database provisioning
- Copy your project URL and anon key
2. Configure Environment Variables
Add your Supabase credentials to your environment variables:
EXPO_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
EXPO_PUBLIC_SUPABASE_ANON_KEY=your-anon-key-here3. Configure Authentication Providers
Magic Link (Email OTP)
- Go to Authentication → Email Templates
- Customize the magic link email template
- Add the OTP code display:
<h2>Confirm your signup</h2> <p>Please enter this code: {{ .Token }}</p>
Google OAuth
- Go to Authentication → Providers → Google
- Enable Google provider
- Add your Google OAuth client ID
- Configure redirect URLs
Apple OAuth
- Go to Authentication → Providers → Apple
- Enable Apple provider
- Add your Apple bundle IDs (iOS app and web)
- Configure Apple service credentials
Facebook/Twitter OAuth
- Go to Authentication → Providers
- Enable the desired provider
- Add OAuth client IDs and secrets
- Configure redirect URLs
4. Database Setup
Initial Migration
If using Prisma with an existing Supabase database:
# Pull existing schema
npx prisma db pull
# Create baseline migration
mkdir -p prisma/migrations/0_init_supabase
# Generate migration
npx prisma migrate diff \
--from-empty \
--to-schema-datamodel prisma/schema.prisma \
--script > prisma/migrations/0_init_supabase/migration.sql
# Mark as applied
npx prisma migrate resolve --applied 0_init_supabaseLink Auth and Public Schema
- Go to Database → Tables
- Create foreign key relationship:
- In your
UserProfiletable, linkuser_idtoauth.users.id
- In your
- Set up Row Level Security (RLS) policies if needed
5. Configure Row Level Security
For tables that should be user-specific:
- Go to Database → Tables → Select table
- Enable Row Level Security
- Create policies:
- Users can only read/update their own data
- Use
auth.uid()to reference the current user
6. Set Up Database Connection
Add database connection strings to environment variables:
DATABASE_URL=postgresql://postgres:[password]@[host]:5432/postgres
DIRECT_URL=postgresql://postgres:[password]@[host]:5432/postgres