Overview
Cajas uses Supabase Auth (powered by GoTrue) for user authentication and authorization. The system implements email/password authentication with automatic profile creation and comprehensive Row Level Security (RLS) policies.Authentication Flow
Implementation
Signup Action
The signup process creates a new user with metadata that triggers profile creation.app/login/actions.ts
Login Action
Login uses email/password authentication with session cookie management.app/login/actions.ts
Signout Action
app/login/actions.ts
OAuth Callback Handler
Handles the OAuth callback after authentication providers redirect back to the app.app/auth/callback/route.ts
Supabase Client Setup
Server-Side Client
For use in Server Components, API routes, and Server Actions.lib/supabase/server.ts
Client-Side Client
For use in Client Components with the'use client' directive.
lib/supabase/client.ts
Automatic Profile Creation
A PostgreSQL trigger automatically creates user profiles when a new auth user is created.supabase/migrations/20240101000000_init.sql
Row Level Security (RLS) Policies
All tables have RLS enabled to ensure users can only access their own data or public data.Users Table Policies
Cases Table Policies
Case Items Table Policies
User Items Table Policies
Transactions Table Policies
User Seeds Table Policies
Game Rolls Table Policies
Admin Logs Table Policies
User Management
Checking Authentication Status
Accessing User Profile
Checking Admin Role
Session Management
Session Cookies
Supabase uses httpOnly cookies to store session tokens:- sb--auth-token: Access token (JWT)
- sb--auth-token.: Additional token chunks if needed
- HttpOnly (not accessible via JavaScript)
- Secure (HTTPS only in production)
- SameSite=Lax (CSRF protection)
Token Refresh
Tokens are automatically refreshed by the Supabase client when they expire. The refresh token is used to obtain a new access token without requiring the user to log in again.Session Duration
Default session duration is 3600 seconds (1 hour) for the access token. Refresh tokens are valid for longer periods and are rotated on each refresh.Security Best Practices
Environment Variables
Store Supabase credentials in environment variables:.env.local
RLS Policy Design
- Always enable RLS on tables containing user data
- Use
auth.uid()to reference the current authenticated user - Write specific policies for SELECT, INSERT, UPDATE, DELETE
- Test policies thoroughly with different user roles
Password Requirements
Supabase Auth enforces:- Minimum 6 characters
- Passwords are hashed with bcrypt before storage
- Rate limiting on auth endpoints
Admin Protection
Admin actions are protected at multiple levels:- Database RLS policies - Check
profiles.role = 'admin' - Application logic - Verify role before rendering admin UI
- API routes - Validate admin status before executing actions
Related Documentation
- System Architecture - Overall system design
- Database Schema - Complete database structure
- API Reference - API endpoints and authentication
