Authentication & Access Control
Procurement Calendar uses Supabase Auth for secure authentication and implements a comprehensive role-based access control (RBAC) system to ensure users only have access to features appropriate for their role.Authentication System
Supabase Auth Integration
The application leverages Supabase’s built-in authentication system with Row-Level Security (RLS) policies to protect data at the database level.Supabase Auth handles session management, password hashing, and secure token storage automatically, ensuring enterprise-grade security.
Server-Side Client
The application creates Supabase clients for server-side operations:lib/supabase/server.ts
Client-Side Client
For browser-based operations:lib/supabase/client.ts
Authentication Flow
User Login
The sign-in process is handled by a server action:lib/actions/auth.ts
- User submits email and password
- Supabase validates credentials
- If successful, session is created and stored in cookies
- User is redirected to
/dashboard/calendar - If failed, error message is returned
User Registration
New users can request access through the sign-up flow:lib/actions/auth.ts
- User fills registration form with name, email, and password
- Account is created in Supabase Auth
- A database trigger automatically creates a profile record
- User is redirected to
/dashboard/pendiente(pending approval page) - Administrator must approve and assign role before user can access the system
User Profile Creation
When a user signs up, a database trigger automatically creates their profile:supabase/schema.sql
Get Current User Profile
Retrieve the authenticated user’s profile:lib/actions/auth.ts
- Gets the authenticated user from Supabase Auth
- Fetches the corresponding profile from the
profilestable - Returns the profile data including role information
Sign Out
Log out the current user:lib/actions/auth.ts
Role-Based Access Control
Available Roles
The system defines roles as a PostgreSQL enum:supabase/schema.sql
While the README mentions additional roles like ‘laboratorio’ and ‘cedis’, the current database schema implements three core roles. Additional roles can be added by extending the enum.
Role Permissions
Here’s a detailed breakdown of what each role can do:Admin
Full System Access
- Manage all users and profiles
- Create, read, update, and delete requisitions
- Manage all catalog tables
- Access audit history
- View all system data
Coordinadora
Requisition Management
- Create new requisitions
- Update existing requisitions
- View all requisitions
- Read catalog data
- Access audit history
- Cannot delete requisitions
Consulta
View-Only Access
- View requisitions
- View calendar
- Read catalog data
- Cannot create or modify any data
- Cannot access admin features
Row-Level Security (RLS)
Supabase RLS policies enforce permissions at the database level, ensuring users can only access data they’re authorized to see.Helper Function
A helper function retrieves the current user’s role:supabase/schema.sql
Profiles Table Policies
supabase/schema.sql
Catalog Tables Policies
All catalog tables (proveedores, productos, presentaciones, destinos, estatus, unidades) share the same policies:
supabase/schema.sql
Requisiciones Table Policies
supabase/schema.sql
| Operation | Admin | Coordinadora | Consulta |
|---|---|---|---|
| SELECT (View) | ✅ | ✅ | ✅ |
| INSERT (Create) | ✅ | ✅ | ❌ |
| UPDATE (Edit) | ✅ | ✅ | ❌ |
| DELETE (Remove) | ✅ | ❌ | ❌ |
Audit History Policies
supabase/schema.sql
Route Protection
The dashboard layout implements route guards to prevent unauthorized access:app/dashboard/layout.tsx
- Pending users are restricted to
/dashboard/pendiente - View-only users (consulta) cannot access protected routes
- Protected routes require admin or coordinadora role
- Unauthorized access attempts redirect to
/dashboard/calendar
Security Best Practices
Environment Variables
- Store Supabase credentials in
.env.local - Never commit sensitive credentials to version control
- Use
NEXT_PUBLIC_prefix only for variables that need to be exposed to the browser
Session Management
- Supabase automatically handles session refresh
- Sessions are stored securely in HTTP-only cookies
- Expired sessions trigger automatic re-authentication
Password Requirements
From the validation schema:- Minimum 6 characters
- Passwords are hashed by Supabase Auth (bcrypt)
- Password confirmation required during registration
API Security
- All API routes check authentication status
- RLS policies enforce database-level security
- Anonymous key (
ANON_KEY) has limited permissions - Service role key should never be exposed to the client
Common Authentication Patterns
Check if User is Authenticated
Check User Role
Protect Server Actions
Troubleshooting
Session Not Persisting
- Ensure cookies are enabled in the browser
- Check that
NEXT_PUBLIC_SUPABASE_URLis correctly set - Verify Supabase project is active
Unauthorized Errors
- Check that RLS policies are properly configured
- Verify user role in the
profilestable - Ensure user is authenticated (check
auth.userstable)
Permission Denied
- Confirm the user has the correct role for the operation
- Check RLS policies in Supabase dashboard
- Verify
get_my_role()function returns the correct role
The authentication and access control system provides enterprise-grade security while maintaining a smooth user experience. All permissions are enforced at multiple layers for defense in depth.
