AuthContext - handles the actual authentication state and user data
AuthModalContext - handles the UI state for the login/signup modal
<Providers>
{' '}
// Theme provider from shadcn
<AuthProvider>
{' '}
// Handles firebase auth state
<AuthModalProvider>
{' '}
// Handles login/signup modal state
<SiteHeader />
{children} // Page content
<Toaster /> // Notifications
</AuthModalProvider>
</AuthProvider>
</Providers>- Uses Firebase authentication
- Manages user state and data
- Provides:
user: Current user data from Firestoreloading: Authentication loading statesignOut: Sign out function
- Manages authentication modal visibility
- Provides:
isOpen: Modal visibility statetype: Current form type ('login', 'register', or 'resetPassword')onOpen: Function to open modalonClose: Function to close modal
-
HomePage:
- Displays login/register buttons if no user
- Shows user stats and dashboard link if logged in
- Uses
useAuthModalto trigger the auth modal
-
AuthModal:
- Renders login/register/reset password forms
- Controlled by AuthModalContext
- Appears as an overlay when triggered
- User clicks "Login"
const handleAuth = (type: 'login') => {
onOpen(type) // Opens modal with login form
}- User clicks "Register"
const handleAuth = (type: 'register') => {
onOpen(type) // Opens modal with registration form
}- User clicks "Forgot Password?" on login form
const handleForgotPassword = () => {
onOpen('resetPassword') // Opens modal with reset form
}- Reset password process:
const handleReset = async (email: string) => {
await sendPasswordResetEmail(email)
// On success: shows confirmation toast and returns to login
// On error: shows error message
}{
type === 'login' ?
: type === 'register' ?
:
}- Successful Login/Register:
// User data is fetched and stored in AuthContext
// Redirects to dashboard
router.push('/dashboard')- Successful Password Reset:
// Email sent confirmation
// Returns to login form
onOpen('login')This structure provides:
- Clear separation of concerns
- Centralized state management
- Consistent auth flow
- Modal UI control
- Comprehensive password recovery
- Seamless form transitions
The system maintains a consistent user experience while handling all authentication scenarios, including password recovery, through a single modal interface controlled by the AuthModalContext.