Skip to main content

Overview

EaseLMS uses Supabase Auth for authentication with custom middleware for session management and role-based access control. The system implements three-tier authentication: server-side, middleware, and client-side.

Architecture

Supabase Client Types

EaseLMS implements three types of Supabase clients for different contexts:

1. Server Client

Used in Server Components and API Routes with cookie-based session management:
lib/supabase/server.ts
The server client automatically reads session cookies and validates authentication state without requiring manual token management.

2. Service Role Client

Used for admin operations that bypass Row Level Security:
lib/supabase/server.ts
The service role client bypasses all RLS policies. Use only for trusted server-side operations like user management and admin tasks.

3. Browser Client

Used in Client Components with automatic session handling:
lib/supabase/client.ts

Middleware Implementation

The middleware handles session validation and route protection:
lib/supabase/middleware.ts

User Roles & Authorization

EaseLMS implements role-based access control with three user types:

User Types

Permissions:
  • Full system access
  • User management
  • Course creation and management
  • Payment and enrollment management
  • Platform settings configuration
  • Analytics access

Role Check in Middleware

middleware.ts (excerpt)

Row Level Security (RLS)

Supabase RLS policies enforce authorization at the database level:

Example: Courses Table

Example: Enrollments Table

Authentication Flows

Login Flow

Signup Flow

Logout Flow

Password Reset Flow

Session Management

Auto-Refresh

Supabase Auth automatically refreshes sessions before expiry:

Manual Session Check

Security Best Practices

Never commit credentials to version control:
Only use service role client for:
  • User management operations
  • Admin-only database operations
  • Bypassing RLS when necessary
Never expose service role key to client-side code.
Always enable RLS on all tables and create policies for:
  • Read access (SELECT)
  • Write access (INSERT, UPDATE, DELETE)
  • Admin override

Debugging Authentication

Enable Debug Logging

middleware.ts

Common Issues

Session Not Persisting

Check that cookies are enabled and HTTPS is used in production.

RLS Blocking Access

Verify RLS policies match your user’s role and use service role client for admin operations.

Redirect Loop

Ensure middleware doesn’t redirect authenticated users accessing valid routes.

Missing User Profile

Verify profile is created on signup (use database trigger or manual insert).

Next Steps

File Storage

Learn about S3 integration for user uploads

Video Processing

Understand video transcoding and streaming