Overview
The/api/presence-ping endpoint allows authenticated users to update their online/offline presence status in the system. This is typically used to track active users, show online indicators, and record last access times.
Use Cases:
- Mark user as online when they log in or become active
- Mark user as offline when they log out or go idle
- Track last access timestamp
- Power online user lists and presence indicators
Endpoint
Authentication
Bearer token of the authenticated user.Format:
Bearer <access_token>Any authenticated user can call this endpoint to update their own presence. Admin permissions are not required.
Request Body
The desired presence state for the user.Allowed values:
online- Mark user as currently active (default)offline- Mark user as inactive or logged out
"online"The request body is optional. If omitted or if
state is not provided, the user will be marked as online by default.Alternative: Query Parameter
Thestate can also be passed as a query parameter:
Response
Success Response (200 OK)
Always
true for successful requests.Updated user presence data from the
usuarios table.Error Responses
401 Unauthorized - Missing Token
Authorization header provided, or header doesn’t start with Bearer .
401 Unauthorized - Invalid Token
404 Not Found - User Not in Database
usuarios table.
Solution: Ensure a user record exists in usuarios with matching correo, nombre_usuario, or auth_user_id.
405 Method Not Allowed
500 Internal Server Error - Missing Environment Variables
500 Internal Server Error - Database Update Failed
Example Requests
Implementation Details
Update Process Flow
-
Authentication Verification
- Extract Bearer token from Authorization header
- Validate token with Supabase Auth
- Retrieve authenticated user data
-
State Determination
- Parse request body or query parameter for
state - Validate state is either
onlineoroffline - Default to
onlineif not specified or invalid
- Parse request body or query parameter for
-
User Lookup
- Search
usuariostable for matching user record - Try multiple identification strategies (see below)
- Return 404 if no match found
- Search
-
Database Update
- Update
ultimo_accesoto current timestamp (ISO 8601) - Update
presence_stateto requested state - If state is
offline, attempt to updatelast_manual_logout_at(if column exists) - Return updated user data
- Update
User Identification Strategy
The endpoint identifies the calling user using multiple fallback methods:- By email (
correo): Match Auth email tousuarios.correo - By username equals email: Match Auth email to
usuarios.nombre_usuario - By username equals local part: Match email local part (before @) to
usuarios.nombre_usuario - By auth_user_id: Direct match to
usuarios.auth_user_id(if column exists)
Database Schema Compatibility
The endpoint is designed to be resilient to schema variations:- If
last_manual_logout_atcolumn doesn’t exist, the update falls back to only updatingultimo_accesoandpresence_state - If
auth_user_idcolumn doesn’t exist, user lookup relies on email/username matching
Timestamp Format
All timestamps are stored in ISO 8601 format:Common Integration Patterns
On Login
Mark user as online immediately after successful authentication:On Logout
Mark user as offline when they explicitly log out:Periodic Heartbeat
Keep user marked as online with periodic pings:On Visibility Change
Update presence when browser tab visibility changes:Best Practices
- Heartbeat Frequency: Send presence pings every 2-5 minutes for active users
- On Logout: Always mark user as offline when they explicitly log out
- Error Handling: Silently fail on presence update errors; don’t block user workflows
- Battery Consideration: Reduce ping frequency on mobile devices to conserve battery
- Stale Detection: Consider users offline if
ultimo_accesois older than 10-15 minutes - Network Resilience: Queue failed presence updates and retry when connection is restored
Environment Variables
Required environment variables:SUPABASE_URL- Supabase project URLSUPABASE_ANON_KEY- Public anonymous keySUPABASE_SERVICE_ROLE_KEYorSUPABASE_SERVICE_KEY- Admin service key
Related Endpoints
- Authentication - How to obtain access tokens
- API Overview - General API concepts and error codes
