Overview
DoctorSoft+ uses Supabase Authentication to provide secure user management with email/password authentication, session handling, and automatic token refresh.Authentication context
The authentication system is built aroundAuthContext which provides authentication state throughout the application.
Using the authentication hook
Authentication state properties
user
user
The current authenticated user object with extended attributes:
id: User’s unique identifieremail: User’s email addressuserRole: User’s role (e.g., “Medico”, “Admin”)idbu: Business unit identifiernombre: User’s display nameestado: User’s account status
loading
loading
Boolean indicating if authentication state is being loaded. Use this to show loading indicators during initial authentication check.
signOut
signOut
Async function to log out the current user. Returns
{ error: AuthError | null }.error
error
Current authentication error, if any. Automatically populated when authentication operations fail.
clearError
clearError
Function to clear the current error state.
Login flow
Login form implementation
The login page (src/pages/Login.tsx) implements a secure login form with validation:
Form validation
The login form uses Zod schema validation to ensure:
- Email is valid format (5-50 characters)
- Password has minimum 8 characters
- Password contains uppercase, lowercase, and numbers
Error handling
The system handles common authentication errors:
- Email not confirmed
- Invalid credentials
- Too many login attempts
- Rate limiting
Login error messages
The system provides user-friendly error messages in Spanish:| Error Type | Message |
|---|---|
| Email not confirmed | ”Por favor confirme su correo electrónico antes de iniciar sesión” |
| Invalid credentials | ”Usuario o contraseña incorrectos” |
| Rate limited | ”Demasiados intentos fallidos. Por favor espere unos minutos” |
| Unknown error | ”Error inesperado. Intente más tarde.” |
Session management
Automatic token refresh
The AuthContext automatically handles token refresh without blocking the UI:When a token refresh occurs (
TOKEN_REFRESHED event), the UI is not blocked. The system updates the session in the background while users continue working.Session validation
For sensitive operations like file uploads, validate the session first:Session caching
User information is cached to reduce database queries:Logout flow
Implement logout functionality using thesignOut function:
Update state
The auth state listener automatically:
- Sets user to
null - Clears the user info cache
- Updates the UI
Password reset
The login page includes a password reset link:Protected routes
Protect routes by checking authentication state:Remember me functionality
The login form includes a “Remember me” checkbox:The remember me functionality stores user preferences locally. Session duration is controlled by Supabase authentication settings.
Best practices
Validate sessions for sensitive operations
Always check session validity before file uploads, data modifications, or API calls.