User Authentication System Documentation
Database: Supabase
The application uses Supabase as its backend database and authentication service. Supabase is a powerful, open-source alternative to Firebase that provides a PostgreSQL database, authentication, instant APIs, and realtime subscriptions.
Key Files and Their Roles
1. lib/supabase.js
This file sets up the Supabase client:
- Imports necessary polyfills and libraries
- Defines Supabase URL and anonymous key
- Creates and exports a Supabase client instance with specific configuration:
- Uses AsyncStorage for persistent session storage
- Enables auto-refresh for authentication tokens
- Persists user sessions
- Sets up an AppState event listener to manage token refreshing based on app state
2. Auth.js (React Native Component)
This file contains the main authentication UI and logic:
- Imports necessary React Native and Supabase components
- Defines the
Authcomponent which includes:- State management for email, password, and loading status
- Functions for signing in and signing up using Supabase authentication
- UI elements for email/password input and submit buttons
How It All Works Together
-
Supabase Client Initialization:
- The
supabase.jsfile initializes the Supabase client with the correct URL and API key. - It sets up persistent storage and auto-refresh for authentication tokens.
- The
-
User Interface:
- The
Auth.jscomponent provides a user interface for sign-in and sign-up. - It manages local state for user input (email and password).
- The
-
Authentication Flow:
- When a user attempts to sign in or sign up, the respective function (
signInWithEmailorsignUpWithEmail) is called. - These functions use the Supabase client (imported from
supabase.js) to interact with Supabase's authentication API. - For sign-in, it uses
supabase.auth.signInWithPassword(). - For sign-up, it uses
supabase.auth.signUp().
- When a user attempts to sign in or sign up, the respective function (
-
Session Management:
- After successful authentication, Supabase handles session management.
- The session is stored persistently using AsyncStorage.
- Token refresh is managed automatically when the app is in the foreground.
-
Error Handling:
- Any errors during authentication are captured and displayed to the user via
Alert.alert().
- Any errors during authentication are captured and displayed to the user via
This setup provides a complete authentication system with a clean separation of concerns:
- Database and API interactions are abstracted through the Supabase client.
- UI and user interactions are handled in the React Native component.
- Authentication state is managed by Supabase, with the app responding to auth state changes.