Authentication Architecture
The authentication system is built using Zustand for state management and Supabase Auth for the authentication backend.The authentication logic is implemented in
/src/stores/AuthStore.ts using Zustand for state management.Key Features
OAuth Authentication
Support for multiple OAuth providers (Google, GitHub, etc.)
Magic Link (OTP)
Passwordless authentication via email
Session Management
Automatic session persistence and renewal
Secure by Default
Row Level Security ensures data isolation
Authentication Store
The application uses Zustand to manage authentication state globally:Available Methods
Setting Up Authentication
Configure Supabase
Ensure you’ve completed the Supabase Setup guide, including:
- Enabling authentication providers
- Configuring OAuth credentials
- Setting redirect URLs
Set Environment Variables
OAuth Provider Setup
Configuring Redirect URLs
When users sign in with OAuth, they’re redirected back to your application. The redirect URL is set automatically:/src/stores/AuthStore.ts:21-23
Supported Providers
The application supports any Supabase OAuth provider:Popular choice for most users
GitHub
Great for developer-focused apps
Microsoft
Enterprise SSO support
Apple
Required for iOS apps
Discord
Gaming communities
Social media integration
Session Management
Checking Authentication State
Access the current session from any component:Persisting Sessions
Supabase automatically persists sessions in local storage. When users return to your app:Protected Routes
Ensure users are authenticated before accessing certain features:Row Level Security
The authentication system integrates with Supabase’s Row Level Security (RLS) to ensure data isolation.When users create bookmarks, the
saved_by field is automatically set to their user ID (auth.uid()).How It Works
- User signs in → Supabase creates a session with a JWT token
- User creates bookmark → The
saved_byfield is set to the user’s ID - User fetches bookmarks → RLS policies filter results to only show bookmarks where
saved_bymatches the user’s ID
/src/stores/BookmarkStore.ts:30-36
Error Handling
The authentication store includes basic error handling:/src/stores/AuthStore.ts:19-29
Consider enhancing error handling with user-friendly toast notifications using the existing toast system at
/src/utils/showToast.ts.Best Practices
Secure Token Storage
Secure Token Storage
Supabase automatically stores tokens securely in local storage. Never manually store tokens in cookies or local storage yourself.
Token Refresh
Token Refresh
Supabase handles token refresh automatically. Sessions are valid for 1 hour and refresh tokens last for 30 days by default.
Sign Out on Token Expiry
Sign Out on Token Expiry
Listen for
SIGNED_OUT events in onAuthStateChange to handle expired sessions gracefully.Testing Authentication
Testing Authentication
Test both OAuth and email OTP flows in development before deploying to production.
Troubleshooting
OAuth redirect not working
OAuth redirect not working
Solutions:
- Verify redirect URLs are added in Supabase Dashboard > Authentication > URL Configuration
- Check that OAuth credentials (Client ID/Secret) are correctly configured
- Ensure your OAuth app’s redirect URI matches Supabase’s callback URL
Magic link not sending
Magic link not sending
Solutions:
- Check Supabase email settings in Authentication > Email Templates
- Verify email provider is configured (default uses Supabase’s SMTP)
- Check spam folder for test emails
- Review logs in Supabase Dashboard for delivery errors
Session not persisting
Session not persisting
Solutions:
- Ensure local storage is enabled in the browser
- Check for conflicting auth logic that might clear sessions
- Verify
onAuthStateChangelistener is properly set up
User can see other users' bookmarks
User can see other users' bookmarks
Solutions:
- Verify Row Level Security is enabled on the bookmarks table
- Check that RLS policies are correctly configured
- Ensure
saved_byfield matchesauth.uid()in queries
Next Steps
Deploy to Production
Deploy your application with authentication
Supabase Setup
Review complete Supabase configuration
/src/stores/AuthStore.ts:18-32The function:truesignInWithOAuth/src/stores/AuthStore.ts:34-45The user receives an email with a magic link to authenticate./src/stores/AuthStore.ts:47-58This clears the session both locally and on Supabase.