Overview
Cajas uses Supabase as its backend platform, providing authentication, database, and Row Level Security (RLS) features. This guide walks you through creating and configuring your Supabase project.Prerequisites
- A Supabase account (sign up at supabase.com)
- Environment variables configured (see Environment Setup)
Create Supabase Project
Create New Project
- Log in to Supabase Dashboard
- Click “New Project”
- Fill in project details:
- Name: cajas-app (or your preferred name)
- Database Password: Generate a strong password and save it securely
- Region: Choose the closest region to your users
- Pricing Plan: Select Free tier for development
Wait for Provisioning
Project creation takes 1-2 minutes. You’ll see a progress indicator while Supabase sets up your database and services.
Get API Credentials
Once ready, navigate to Settings > API and copy:
- Project URL - Your Supabase project URL
- anon public key - Public API key for client access
.env.local file as described in Environment Setup.Enable Required Services
Enable Authentication
Cajas uses Supabase Auth for user management.Navigate to Authentication > Providers and configure:Email Provider (Default - Already Enabled)
- Confirm email: Enable for production
- Secure email change: Enable
Configure Email Templates
Customize authentication emails:Go to Authentication > Email Templates and edit:
- Confirm signup
- Reset password
- Magic link
Database Configuration
Required Extensions
Cajas migrations automatically enable required PostgreSQL extensions:supabase/migrations/20240101000000_init.sql:2 for UUID generation.
Row Level Security (RLS)
All tables in Cajas use RLS for security. The migrations automatically:- Enable RLS on all tables
- Create appropriate policies for each table
- Restrict admin actions to users with
role = 'admin'
RLS is automatically configured when you run migrations. See Database Migrations for details.
Database Schema Overview
Cajas uses the following main tables:users
User profiles with balance, username, and provably fair seeds.Key Fields:
id, username, balance, client_seed, noncecases
Available loot cases with pricing and metadata.Key Fields:
id, name, slug, price, image_urlcase_items
Items available in each case with drop probabilities.Key Fields:
case_id, name, value, probabilityuser_seeds
Provably fair gaming seeds per user.Key Fields:
user_id, server_seed, client_seed, noncegame_rolls
Audit log of all game rolls for fairness verification.Key Fields:
user_id, case_id, roll_result, item_won_idadmin_logs
Admin action audit trail.Key Fields:
admin_id, action, detailsRLS Policy Configuration
The application implements security through Row Level Security:Public Read Access
supabase/migrations/20240101000000_init.sql:44-46 and 0000_create_cases_system.sql:41-53.
User-Specific Access
supabase/migrations/20251209000000_create_provably_fair.sql:15-17.
Admin-Only Access
supabase/migrations/0000_create_cases_system.sql:42-44.
Testing Database Connection
Troubleshooting
Connection timeout
Connection timeout
Symptoms: Requests hang or timeoutSolutions:
- Verify your project is not paused (Free tier pauses after inactivity)
- Check your internet connection
- Confirm the project URL is correct in
.env.local - Try restarting your Supabase project from the dashboard
Invalid JWT / Auth errors
Invalid JWT / Auth errors
Symptoms:
Invalid JWT or authentication errorsSolutions:- Clear browser cookies and local storage
- Verify you’re using the
anonkey, not theservice_rolekey - Check that RLS policies are correctly configured
- Ensure the JWT secret hasn’t been rotated
RLS policy violations
RLS policy violations
Symptoms:
new row violates row-level security policySolutions:- Check that the user is authenticated:
auth.uid()should return a value - Verify the user has the correct role (e.g.,
adminfor admin operations) - Review policy conditions in your migration files
- Test policies in Supabase SQL Editor
Migration conflicts
Migration conflicts
Symptoms: Errors when running migrationsSolutions:
- Check if tables already exist from previous migrations
- Review migration order (files are run alphabetically)
- Use
IF NOT EXISTSclauses in CREATE statements - See Database Migrations for details
Performance Optimization
Connection Pooling
Supabase automatically handles connection pooling. Use the provided client helpers (
lib/supabase/client.ts and lib/supabase/server.ts) for optimal performance.Indexes
Add indexes for frequently queried columns:
Caching
Use
@supabase/ssr for automatic cookie-based caching in Next.js (already configured).Query Optimization
Select only needed columns:
Next Steps
Database Migrations
Run migrations to set up your database schema
Authentication Setup
Configure user authentication and authorization
