Overview
The Hive API uses Bearer token authentication powered by Supabase Auth. All API endpoints require a valid access token in theAuthorization header.
Authentication Flow
1. Obtain an Access Token
Access tokens are issued by Supabase Auth through standard authentication flows: Email/Password Sign In:2. Include Token in API Requests
Once you have an access token, include it in theAuthorization header of all API requests:
Authorization Header Format
The API expects theAuthorization header in the following format:
Bearer token for authentication. Must start with
Bearer followed by the access token.Format: Bearer <access_token>Example: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8UToken Validation
When you make an API request, the server:- Extracts the token from the
Authorizationheader - Creates a Supabase client with the token to verify authenticity
- Calls
supabase.auth.getUser()to validate the token and retrieve user information - Matches the authenticated user to a record in the
usuariostable
401 Unauthorized error.
Token Expiration and Refresh
Access Token Lifespan
Supabase access tokens typically expire after 1 hour. When a token expires, API requests will return:Refreshing Tokens
Use the refresh token to obtain a new access token:User Roles and Permissions
The Hive API implements role-based access control:Standard User
- Can access their own user data
- Can update their presence status
- Endpoints:
/api/presence-ping
Administrator
Administrator access is determined by:-
Role in usuarios table: User must have
rolfield set to one of:administradoradminsuperadminsuper-admin
-
Admin whitelist: Email listed in
ADMIN_EMAILSenvironment variable (comma-separated)
- Sync user authentication credentials
- Update any user’s email, password, or username
- Access all standard user endpoints
/api/auth-sync, /api/presence-ping
User Identification
The API identifies users in theusuarios table using multiple fallback strategies:
- By
auth_user_id- Direct match to Supabase Auth user ID - By
correo- Email address from Auth matches email in usuarios - By
nombre_usuario- Username equals full email address - By
nombre_usuario- Username equals local part of email (before@)
Error Responses
401 Unauthorized - Missing Token
Authorization header provided, or header doesn’t start with Bearer .
401 Unauthorized - Invalid Token
403 Forbidden - Insufficient Permissions
usuarios table or is whitelisted in ADMIN_EMAILS.
404 Not Found - User Not in Database
usuarios table.
Solution: Create a corresponding record in usuarios with matching correo or auth_user_id.
Best Practices
- Secure Token Storage: Never expose access tokens in client-side code or version control
- Token Refresh: Implement automatic token refresh to maintain seamless API access
- Error Handling: Always handle 401 errors by refreshing tokens and retrying
- HTTPS Only: Never send tokens over unencrypted HTTP connections
- Token Revocation: Call
supabase.auth.signOut()when users log out to invalidate tokens
Testing Authentication
To test your authentication setup:Environment Configuration
Ensure these environment variables are set for authentication to work:SUPABASE_URL- Your Supabase project URLSUPABASE_ANON_KEY- Supabase anonymous key (safe for client-side use)SUPABASE_SERVICE_ROLE_KEY- Supabase service role key (server-side only, never expose)ADMIN_EMAILS- Comma-separated list of admin emails (optional)
Next Steps
- Review /api/auth-sync endpoint documentation
- Review /api/presence-ping endpoint documentation
- Explore the API Overview for general concepts
