AuthContext provides centralized authentication state management for BoxApp. It handles user sessions, profiles, authentication methods, and role-based access control.
Provider Setup
Wrap your application withAuthProvider to enable authentication throughout your component tree.
Optional box ID for multi-tenant context. When provided, new user registrations and OAuth flows will automatically associate users with this box.
Hook Usage
Access the authentication context using theuseAuth hook.
The
useAuth hook must be used within an AuthProvider. An error will be thrown if used outside the provider tree.State Properties
The context exposes the following state properties:The current Supabase session object. Contains tokens, user data, and session metadata.
The authenticated Supabase user object. Contains user ID, email, and metadata.
The user’s profile data from the
profiles table.The box (gym) settings associated with the user’s profile. Automatically loaded when the user profile contains a
box_id.Indicates whether authentication state is being initialized or updated. Use this to show loading states during sign-in, sign-up, or profile refresh operations.
Role-Based Flags
Convenience boolean flags for role-based access control:Returns
true if the user’s role_id is 'admin'.Returns
true if the user’s role_id is 'coach'.Returns
true if the user’s role_id is 'athlete'.Returns
true if the user’s email is '[email protected]' or user_metadata.is_root is true. Used for super-admin access.Authentication Methods
signIn
Sign in a user with email and password.Authentication credentials object.
Promise<{ error: any; data?: any }>
signInWithGoogle
Initiate Google OAuth sign-in flow.Optional box ID to associate with the user after OAuth completion. Stored in localStorage as
pending_box_id and reconciled after redirect.Promise<{ error: any }>
The OAuth flow redirects to
/auth/callback. The onAuthStateChange listener handles session and profile fetch after redirect.signUp
Register a new user account.User registration credentials.
Promise<{ data: any; error: any }>
If
tenantBoxId was provided to the AuthProvider, it will automatically be injected into options.data.box_id during registration.resetPassword
Send a password reset email to the user.Email address to send the password reset link to.
Promise<{ error: any }>
The reset link redirects to
/reset-password on your application domain.updateUser
Update the current user’s attributes.User attributes to update (e.g.,
email, password, data).Promise<{ data: any; error: any }>
signOut
Sign out the current user and clear session state.Promise<void>
refreshProfile
Manually refresh the user’s profile and box data from the database.Promise<void>
setCurrentBox
Manually set the current box in context.The box object to set as current, or
null to clear.Multi-Tenant Behavior
TheAuthContext includes built-in support for multi-tenant architecture:
-
OAuth Box Association: When
signInWithGoogleis called with aboxId(ortenantBoxIdis available), the box ID is stored inlocalStorageaspending_box_idand reconciled after OAuth redirect. -
Profile Box Reconciliation: If a user’s profile lacks a
box_idbut the provider has atenantBoxId, the profile is automatically updated with the correct box association. -
Sign-Up Box Injection: During registration, if
tenantBoxIdis provided to the provider, it’s automatically added to the user’s metadata.