Skip to content

Latest commit

 

History

History
183 lines (146 loc) · 4.88 KB

File metadata and controls

183 lines (146 loc) · 4.88 KB

Toast Duplicate Fix Summary

Issue Fixed

Problem: Toast notifications were showing twice for the same error.

Root Cause: Toasts were being shown in two places:

  1. When the error first occurred (inside try block)
  2. Again in the catch block

Solution

Removed duplicate toast calls from the catch blocks in AuthContext.tsx. Now each error shows only ONE toast notification.

Changes Made

File: frontend/src/contexts/AuthContext.tsx

1. Login Function (Lines 194-198)

Before:

} catch (error) {
  console.error('Login error:', error);
  const message = error instanceof Error ? error.message : 'Login failed';
  showToast({ title: 'Login error', description: message, variant: 'error' }); // ❌ Duplicate
  throw error;
}

After:

} catch (error) {
  console.error('Login error:', error);
  // Don't show toast here - it was already shown above ✅
  throw error;
}

2. Register Function (Lines 256-260)

Before:

} catch (error) {
  console.error('Registration error:', error);
  const message = error instanceof Error ? error.message : 'Registration failed';
  showToast({ title: 'Signup error', description: message, variant: 'error' }); // ❌ Duplicate
  throw error;
}

After:

} catch (error) {
  console.error('Registration error:', error);
  // Don't show toast here - it was already shown above when the specific error occurred ✅
  throw error;
}

How It Works Now

Login Error Flow:

  1. User enters invalid credentials
  2. Supabase returns error
  3. Toast shown: "Login error - Invalid login credentials" ✅ (Once)
  4. Error thrown and logged
  5. Component handles the error (no additional toast)

Signup Error Flow:

  1. User tries to register with existing email
  2. Database returns constraint error
  3. Toast shown: "Signup error - This email is already registered..." ✅ (Once)
  4. Error thrown and logged
  5. Component handles the error (no additional toast)

Success Flow (Unchanged):

  1. User logs in successfully
  2. Profile fetched successfully
  3. Toast shown: "Login successful - Welcome back!" ✅ (Once)
  4. User redirected to homepage

Toast Locations (Single Source)

Login Errors:

  • ✅ Line 158: Initial auth error (invalid credentials)
  • ✅ Line 182: Profile fetch error (user not found)
  • ❌ Line 197: Removed (was duplicate)

Signup Errors:

  • ✅ Line 212: Auth signup error
  • ✅ Line 245: Database insert error (with specific error messages)
  • ❌ Line 260: Removed (was duplicate)

Success Messages:

  • ✅ Line 188: Login success
  • ✅ Line 255: Signup success
  • ✅ Line 318: Logout success

Testing Results

Before Fix:

User enters wrong password
→ Toast 1: "Login error - Invalid login credentials"
→ Toast 2: "Login error - Invalid login credentials" ❌ Duplicate

After Fix:

User enters wrong password
→ Toast: "Login error - Invalid login credentials" ✅ Only once

Specific Error Messages (No Duplicates):

  • ❌ Invalid credentials → 1 toast
  • ❌ Email already exists → 1 toast
  • ❌ PRN already registered → 1 toast
  • ❌ Username already taken → 1 toast
  • ❌ User profile not found → 1 toast
  • ✅ Login successful → 1 toast
  • ✅ Signup successful → 1 toast
  • ✅ Logout successful → 1 toast

Code Pattern

✅ Correct Pattern:

try {
  const result = await someOperation();
  if (error) {
    showToast({ title: 'Error', description: message, variant: 'error' });
    throw new Error(message);
  }
  showToast({ title: 'Success', description: message, variant: 'success' });
} catch (error) {
  console.error('Operation error:', error);
  // Don't show toast here - already shown above
  throw error;
}

❌ Wrong Pattern (Causes Duplicates):

try {
  const result = await someOperation();
  if (error) {
    showToast({ title: 'Error', description: message, variant: 'error' }); // First toast
    throw new Error(message);
  }
} catch (error) {
  console.error('Operation error:', error);
  showToast({ title: 'Error', description: message, variant: 'error' }); // Duplicate toast ❌
  throw error;
}

Benefits

  1. Better UX: Users see error message only once (cleaner)
  2. Less Clutter: No duplicate notifications stacking up
  3. Clearer Feedback: Single, clear error message
  4. Professional: Consistent notification behavior

Testing Checklist

  • Login with wrong password → 1 error toast
  • Login with non-existent email → 1 error toast
  • Signup with existing email → 1 error toast
  • Signup with existing PRN → 1 error toast
  • Signup with existing username → 1 error toast
  • Successful login → 1 success toast
  • Successful signup → 1 success toast
  • Successful logout → 1 success toast

Summary

Fixed: Toast notifications now show exactly once per event - no more duplicates!


Last Updated: October 2, 2025