Overview
BoxApp uses Supabase Auth for authentication, providing secure user management with support for email/password authentication and OAuth providers like Google. The authentication system is integrated with user profiles and role-based access control.Authentication Context
TheAuthContext provides a React hook for managing authentication throughout your application:
Auth Context Interface
The current Supabase session object, or
null if not authenticated.The current authenticated user from Supabase Auth.
The user’s profile record from the
profiles table, including role and box association.The user’s associated box/gym settings and configuration.
true while authentication state is being loaded or updated.true if the user has the admin role.true if the user has the coach role.true if the user is a root/super admin (root@test.com or is_root metadata).true if the user has the athlete role.Sign In
Authenticate a user with email and password.Parameters
Returns
An error object if authentication failed, otherwise
null.Contains the user and session data on successful authentication.
The
signIn function automatically fetches the user’s profile and box settings after successful authentication.Sign In with Google
Authenticate using Google OAuth.Parameters
The box ID to associate with the user after OAuth authentication. If provided, it will be stored and reconciled after the OAuth redirect.
Returns
An error object if the OAuth flow failed to initiate, otherwise
null.Sign Up
Create a new user account.Parameters
Returns
Contains the new user and session information.
An error object if registration failed, otherwise
null.In multi-tenant environments, the
box_id is automatically injected from the tenant context during sign-up.Password Reset
Initiate a password reset flow for a user.Parameters
The email address of the account to reset
Returns
An error object if the password reset request failed, otherwise
null.The password reset email will contain a link to
/reset-password in your application.Update User
Update the authenticated user’s email or password.Parameters
Returns
Contains the updated user information.
An error object if the update failed, otherwise
null.Sign Out
End the current user session.The
signOut function automatically clears the session and resets the authentication state.Refresh Profile
Manually refresh the user’s profile and box data from the database.Set Current Box
Manually set the current box context.Parameters
The box object to set as current, or
null to clear.User Profiles
After authentication, user profiles are stored in theprofiles table with the following structure:
Accessing Profile Data
Updating Profile Data
Role-Based Access Control
BoxApp implements role-based permissions:Roles
Full access to all box management features, settings, and user management
Can manage classes, workouts, and view member information
Standard member access to book classes, view workouts, and track PRs
Super admin with cross-box access (system administrators only)
Checking Roles
Multi-Tenant Authentication
BoxApp supports multi-tenancy where each box/gym is a separate tenant:Tenant Context
When using theAuthProvider, you can specify a tenantBoxId:
- New users are automatically associated with the box
- OAuth callbacks reconcile the box association
- Profile queries respect box boundaries
Box Reconciliation
After OAuth authentication, BoxApp automatically reconciles the user’s box association:- Checks for a pending box ID from localStorage
- Falls back to the tenant context box ID
- Updates the user’s profile if needed
- Clears temporary storage
Session Management
Supabase automatically manages session tokens and refresh tokens:Getting Current Session
Listening for Auth Changes
Auth Events
SIGNED_IN- User signed inSIGNED_OUT- User signed outTOKEN_REFRESHED- Session token refreshedUSER_UPDATED- User data updatedPASSWORD_RECOVERY- Password reset initiated
Security Best Practices
Row-Level Security
BoxApp uses PostgreSQL row-level security (RLS) to ensure users can only access data within their box:Password Requirements
- Minimum 6 characters (enforced by Supabase Auth)
- Consider implementing client-side validation for stronger passwords
Email Verification
Configure email verification in your Supabase project settings to require users to confirm their email before accessing the application.Error Codes
Common authentication error codes:| Code | Description |
|---|---|
invalid_credentials | Email or password is incorrect |
email_not_confirmed | User hasn’t verified their email |
user_already_exists | Email is already registered |
weak_password | Password doesn’t meet requirements |
over_request_rate_limit | Too many requests, try again later |
Next Steps
- Learn about API Overview for general API usage
- Explore Supabase Auth Documentation for advanced features
- Configure row-level security policies in your Supabase dashboard