Overview
The Create User Admin Edge Function provides a secure API for administrators to create new user accounts with specific roles and organization assignments. This function bypasses normal user signup flows and allows immediate account activation with pre-assigned roles. Endpoint:/functions/v1/create-user-admin
Authentication: Required (Authorization header with Supabase JWT)
Method: POST
Permissions: Only users with global_admin, org_admin, or support roles can create users
Request Format
Headers
Supabase JWT token:
Bearer {token}Must be
application/jsonBody Parameters
Email address for the new user account. Must be unique in the system.
Initial password for the user account. User can change this after first login.
Full name of the user to be displayed in the system.
Role to assign to the user. Valid values:
usuario, soporte, org_admin, global_adminUUID of the organization to assign the user to. If not provided, uses the caller’s organization.
Response Format
Success Response (200)
Always
true on successful user creationInformation about the created user
Error Response (400)
Error message describing what went wrong
TypeScript Interface
Fromsupabase/functions/create-user-admin/index.ts:10-16:
Example Usage
Create Global Admin User
Create Organization Admin
When
organization_id is omitted, the new user is assigned to the same organization as the caller.Authorization Logic
The function implements role-based authorization fromsupabase/functions/create-user-admin/index.ts:56-59:
- global_admin - Can create users in any organization
- org_admin - Can create users in their own organization
- support - Can create users as part of support operations
Implementation Details
User Creation Flow
- Authentication Check - Validates the Authorization header contains a valid JWT token
- Permission Verification - Confirms the caller has one of the allowed roles (
global_admin,org_admin, orsupport) - User Creation - Creates the user account using Supabase Admin API with email confirmation automatically enabled
- Profile Update - Updates the user’s profile record with the specified organization and role
- Response - Returns the new user’s ID, email, and full name
Email Confirmation
Users created via this function haveemail_confirm: true set automatically (line 73), meaning they can log in immediately without requiring email verification.
Password Requirements
The function accepts any password provided. It’s recommended to enforce strong password policies at the application level before calling this function.Error Handling
Common error scenarios:| Error Message | Cause | Resolution |
|---|---|---|
| ”No authorization header” | Missing Authorization header | Include valid JWT token in Authorization header |
| ”Unauthorized” | Invalid or expired token | Refresh authentication token |
| ”Insufficient permissions to create users” | Caller lacks required role | Only admins and support can create users |
| ”Could not verify caller permissions” | Profile lookup failed | Ensure caller has a valid profile record |
| ”Failed to create user” | User creation failed | Check if email already exists or other validation errors |
Security Considerations
- Service Role Key: Uses
SUPABASE_SERVICE_ROLE_KEYto create users (line 31) - CORS: Accepts requests from any origin (
Access-Control-Allow-Origin: *) - Session Isolation: Uses
persistSession: falseto prevent session leakage - Role Validation: Enforces strict role checking before allowing user creation
Related Documentation
Environment Variables
Required environment variables (automatically available in Supabase Edge Functions):SUPABASE_URL- Your Supabase project URLSUPABASE_SERVICE_ROLE_KEY- Service role key for admin operationsSUPABASE_ANON_KEY- Anonymous key for caller authentication