Overview
EaseLMS uses Supabase authentication for API security. Most API endpoints require a valid Supabase authentication token in the request headers.Authentication Flow
The API uses session-based authentication with Supabase:- User logs in via the
/api/auth/loginendpoint - Supabase creates a session and returns access tokens
- Subsequent API requests include the session cookie
- Middleware validates the session on protected routes
User Types
EaseLMS supports three user types:user- Learners who can enroll in and take coursesinstructor- Instructors who can create and manage coursesadmin- Administrators with full system access
Login
Endpoint
Request Body
User’s email address
User’s password
Type of user attempting to login:
user, instructor, or adminExample Request
Success Response
The authenticated user object from Supabase
The Supabase session object containing authentication tokens
Example Success Response
Error Responses
Invalid Credentials
401 Unauthorized
Wrong User Type
If a learner tries to login through the admin portal:403 Forbidden
If an admin tries to login through the learner portal:
403 Forbidden
Instructors can login through the admin portal and will be granted access.
Making Authenticated Requests
Using Session Cookies
After successful login, Supabase automatically sets session cookies. Include these cookies in subsequent requests:Using Authorization Header
Alternatively, you can use the access token in the Authorization header:Checking Authentication Status
Protected endpoints will return401 Unauthorized if the session is invalid or expired:
User Permissions
Different endpoints require different permission levels:Public Endpoints
These endpoints don’t require authentication:GET /api/courses(published courses only)GET /api/courses/[id](published courses only)
Authenticated User Endpoints
These endpoints require any authenticated user:GET /api/enrollments(returns user’s own enrollments)POST /api/enrollments(self-enrollment)GET /api/progressPOST /api/progressGET /api/profile
Admin/Instructor Endpoints
These endpoints require admin or instructor privileges:POST /api/courses(admin only)GET /api/courses?all=true(all courses including drafts)GET /api/users(admin only)POST /api/users(admin only)GET /api/admin/stats(admin only)
Permission Denied Response
If a user lacks required permissions:403 Forbidden
Signup
Endpoint
Request Body
New user’s email address
New user’s password
New user’s full name
Type of user account:
user, instructor, or adminExample Request
Logout
Endpoint
Example Request
Password Reset
Request Password Reset
Change Password
Row Level Security (RLS)
EaseLMS uses Supabase Row Level Security to protect data at the database level. The API includes two types of Supabase clients:Regular Client
Used for user-scoped operations. RLS policies apply:- Users can only access their own enrollments
- Users can only update their own progress
- Users can only view their own profile
Service Role Client
Used for admin operations and bypasses RLS:- Admin user management
- Cross-user enrollment operations
- System-wide statistics
Security Best Practices
Always use HTTPS in production
Always use HTTPS in production
Never send authentication credentials over unencrypted HTTP connections.
Store tokens securely
Store tokens securely
If storing tokens on the client, use secure, httpOnly cookies or secure storage mechanisms.
Validate user types
Validate user types
The API validates user types on login to prevent unauthorized access to admin/instructor features.
Handle token expiration
Handle token expiration
Implement token refresh logic to handle expired sessions gracefully.
Never expose service role keys
Never expose service role keys
Keep
SUPABASE_SERVICE_ROLE_KEY private and only use it server-side.