Your Firebase authentication was not persisting across page refreshes or navigation. This caused the Firestore rules to deny access because:
- User logged in → Auth state stored in memory
- Page refresh → Auth state lost
- Firestore rules check
request.auth != null→ Failed (no auth context) - Permission denied → "Missing or insufficient permissions" error
Firebase Auth in Next.js defaults to NONE persistence, which means:
- Auth state is only kept in memory during the current session
- Any page refresh, navigation, or window reload clears the auth context
- The Firestore rules can't verify the user's identity
Added setPersistence() to the Firebase initialization:
import { setPersistence, browserLocalPersistence } from 'firebase/auth';
// Inside FirebaseProvider
setPersistence(auth, browserLocalPersistence).catch((error) => {
console.warn("Failed to set Firebase auth persistence:", error);
});What this does:
- Stores authentication token in browser's
localStorage - Automatically restores auth state on page reload
- Persists until user explicitly signs out
Also added persistence to the initializeFirebase() function for consistency:
if (typeof window !== 'undefined') {
setPersistence(auth, browserLocalPersistence).catch((error) => {
console.warn("Failed to set Firebase auth persistence:", error);
});
}Why both locations:
provider.tsx: Client-side initialization (React context)lib/firebase.ts: Server action initialization (form submissions, data operations)
-
User logs in
→ signInWithEmailAndPassword() called → Token stored in localStorage (via setPersistence) → Auth context updated -
Page refresh or navigate
→ Firebase automatically restores auth from localStorage → onAuthStateChanged() fires with restored user → Firestore rules validate auth successfully → User data loads without permission errors -
User logs out
→ signOut() called → localStorage auth token cleared → Auth state set to null
This fix enables persistent access to:
✅ Login → Profile page (auth state maintained across pages)
✅ Profile edit (Firestore updates work with persistent auth)
✅ Challenge page (can fetch user data without re-authentication)
✅ Connections (read/write operations maintain auth context)
✅ Page refreshes (user stays logged in)
- Login at
/login - Navigate to
/profile→ Should load without permissions error - Refresh page → Should still be logged in
- Edit profile → Should save successfully
- Go to challenge → Should load challenges without errors
- Close browser/reopen → Session persists (browser localStorage)
browserLocalPersistence works in:
- ✅ Chrome, Firefox, Safari, Edge
- ✅ Mobile browsers
- ❌ Private/Incognito mode (localStorage disabled - falls back to session)
- localStorage access: Accessible to JavaScript on the same domain
- Token expiration: Firebase handles automatic token refresh
- Logout required: Auth persists until user calls
signOut() - No sensitive data in localStorage: Only the auth token (JWT), not passwords
After deploying these changes:
- Users can log in
- Auth persists after page refresh
- Profile can be edited without permission errors
- Challenge page loads successfully
- Logout clears the session
- Firestore reads/writes work smoothly
If issues persist, check:
- Browser dev tools → Application/Storage → localStorage (should have Firebase tokens)
- Browser console for any
setPersistencewarnings - Firestore rules still allow authenticated reads/writes