Overview
The/api/auth-sync endpoint allows administrators to update a user’s email, password, or username in Supabase Auth and automatically mirror those changes to the usuarios table. This ensures consistency between authentication data and application user records.
Use Cases:
- Updating user email addresses
- Resetting user passwords
- Changing usernames
- Synchronizing Auth and database records
Endpoint
Authentication
Bearer token of an authenticated administrator.Format:
Bearer <access_token>Permission Requirements
The caller must meet one of the following criteria:-
Have a
rolfield inusuariostable set to:administradoradminsuperadminsuper-admin
-
Be listed in the
ADMIN_EMAILSenvironment variable
Request Body
The ID of the user in the
usuarios table to update.Example: 123New email address for the user. Must be unique across all users.Example:
[email protected]New password for the user. Updates only the Supabase Auth password.Example:
newSecurePassword123!New username for the user. Must be unique across all users.Example:
john_doe_2024At least one of
email, password, or username must be provided. Sending an empty request body will result in a 400 error.Response
Success Response (200 OK)
Always
true for successful requests.Supabase Auth user data after the update.
Updated user record from the
usuarios table. Only included if email or username was changed.Error Responses
400 Bad Request - Invalid User ID
id field is missing, not a number, or invalid.
400 Bad Request - No Data to Update
email, password, or username.
400 Bad Request - Auth Update Failed
401 Unauthorized - Missing Token
Authorization header or header doesn’t start with Bearer .
401 Unauthorized - Invalid Token
403 Forbidden - Not an Administrator
404 Not Found - User Not Found in Database
usuarios table with the provided id.
404 Not Found - Auth User Not Found
usuarios table.
The endpoint searches through the first 200 Auth users. If your system has more than 200 users, pagination may need to be implemented.
409 Conflict - Username Exists
username is already taken by another user.
409 Conflict - Email Exists
email is already registered to another user.
500 Internal Server Error - Missing Environment Variables
500 Internal Server Error - Database Error
Example Requests
Implementation Details
Update Process Flow
-
Authentication Verification
- Extract Bearer token from Authorization header
- Validate token with Supabase Auth
- Retrieve caller’s user data
-
Permission Check
- Query
usuariostable for caller’s role - Check if role is admin (administrador, admin, superadmin, super-admin)
- Check if email is in ADMIN_EMAILS whitelist
- Return 403 if neither condition is met
- Query
-
Target User Lookup
- Find user in
usuariostable by providedid - Return 404 if user doesn’t exist
- Extract current email (
correo)
- Find user in
-
Auth User Matching
- Search Supabase Auth users for matching email
- Return 404 if no Auth user found
-
Duplicate Validation
- If updating username: check for existing
nombre_usuario - If updating email: check for existing
correo - Return 409 conflict if duplicates found
- If updating username: check for existing
-
Update Operations
- Update Supabase Auth user (email, password, user_metadata.username)
- Mirror changes to
usuariostable (correo, nombre_usuario) - Return success with updated data
User Identification Strategy
The endpoint uses multiple fallback methods to identify the caller:- Match by
auth_user_idcolumn (if exists) - Match by
correoequals Auth email - Match by
nombre_usuarioequals full Auth email - Match by
nombre_usuarioequals local part of Auth email (before @)
Environment Variables
SUPABASE_URL- Supabase project URLSUPABASE_ANON_KEY- Public anonymous keySUPABASE_SERVICE_ROLE_KEYorSUPABASE_SERVICE_KEY- Admin service keyADMIN_EMAILS- Comma-separated admin email whitelist (optional)
Best Practices
- Validate Input: Always validate email format and password strength before calling the API
- Handle Conflicts: Implement proper error handling for 409 conflicts (duplicate username/email)
- Security: Never expose service role keys in client-side code
- User Feedback: Provide clear feedback to users when updates fail
- Audit Trail: Consider logging all auth-sync operations for security auditing
Related Endpoints
- Authentication - How to obtain admin access tokens
- API Overview - General API concepts and error codes
