Documentation Index
Fetch the complete documentation index at: https://mintlify.com/DavidCevallos15/Crucidrive---APP/llms.txt
Use this file to discover all available pages before exploring further.
useSupabaseAuth encapsulates all authentication logic for CruciDrive. On mount it reads the active Supabase session, subscribes to onAuthStateChange events, and keeps the Zustand useAuthStore in sync. It exposes the full Phone OTP flow — sending the SMS code, verifying it, registering a new profile against the Express backend, and signing out — so every screen in the app can share a single, consistent auth state.
This hook internally uses
authFetch — a thin fetch wrapper that automatically attaches the current Bearer token from the Zustand store — for profile registration calls to the Express backend.Signature
Return values
| Property | Type | Description |
|---|---|---|
session | Session | null | Active Supabase auth session, including the JWT access token |
user | User | null | Supabase auth.users record for the current session |
profile | UserProfile | null | Row from the perfiles table — includes nombre, telefono, rol, calificacion |
isLoading | boolean | true while any async auth operation is in progress |
isInitialized | boolean | true once the initial getSession() call has completed |
otpSent | boolean | true after sendOtp completes successfully |
authError | string | null | Human-readable error message from the last failed operation |
sendOtp | (phone: string) => Promise<boolean> | Sends an SMS OTP to the given phone number |
verifyOtp | (phone: string, code: string) => Promise<boolean> | Verifies the SMS code and establishes a session |
registerProfile | (data) => Promise<boolean> | POSTs profile data to the Express backend |
signOut | () => Promise<void> | Signs out and clears the Zustand session |
fetchProfile | (userId: string) => Promise<UserProfile | null> | Queries the perfiles table for a user ID |
setAuthError | (error: string | null) => void | Manually set or clear the auth error message |
setOtpSent | (sent: boolean) => void | Manually reset the otpSent flag |
sendOtp(phone)
Calls supabase.auth.signInWithOtp({ phone }) to trigger an SMS code to the supplied number. The phone number must include the international dialling prefix.
Returns Promise<boolean> — true if the SMS was dispatched without error, false otherwise. On failure, authError is set to the Supabase error message.
verifyOtp(phone, code)
Calls supabase.auth.verifyOtp({ phone, token: code, type: 'sms' }). On success the returned Session is written to the Zustand store via setSession, making the JWT immediately available to authFetch and useSocket.
Returns Promise<boolean> — true if the session was established, false otherwise.
registerProfile(profileData)
POSTs { nombre, rol } to POST /api/auth/registro using authFetch (Bearer token auto-attached). On a successful 2xx response, fetchProfile is called immediately so profile reflects the new row without a page reload.
The user’s display name, e.g.
"Ana Mendoza".The account role. Determines which navigation group (
(passenger) or (driver)) the user is routed to after login.Promise<boolean> — true if the backend created or updated the profile successfully.
signOut()
Calls supabase.auth.signOut() then clearSession() in the Zustand store. This removes the JWT, sets session and user to null, and causes useSocket to disconnect automatically (it watches session?.access_token).
Session initialization
On mount, auseEffect runs the following sequence:
- Calls
supabase.auth.getSession()to restore any persisted session fromAsyncStorage. - If a session exists, calls
fetchProfile(session.user.id)to populateprofile. - Sets
isInitialized: trueonce both calls complete (regardless of outcome). - Subscribes to
supabase.auth.onAuthStateChangeto react to token refreshes and sign-out events. The subscription is cleaned up when the hook unmounts.
isInitialized to avoid a flash of the login screen when the app resumes with an active session.