fix: add timeout to prevent auth loading from hanging indefinitely

This commit is contained in:
nmemmert committed 2026-01-15 14:06:33 -05:00
1 parent 9c73674c84
commit 8a5658a359
3 files changed
+19 -61

No files matched your search

+1 -4
View File
@@ -24,7 +24,6 @@ interface Prayer {
export default function Home() {
const [activeTab, setActiveTab] = useState<'home' | 'journal' | 'reflections' | 'settings'>('home');
const [prayers, setPrayers] = useState<Prayer[]>([]);
const [loading, setLoading] = useState(true);
const [editingPrayer, setEditingPrayer] = useState<Prayer | null>(null);
// Form state
@@ -53,8 +52,6 @@ export default function Home() {
} catch (error) {
console.error('Error loading prayers:', error);
toast.error('Failed to load prayers');
} finally {
setLoading(false);
}
};
@@ -147,7 +144,7 @@ export default function Home() {
}
};
if (authLoading || loading) {
if (authLoading) {
return (
<div className="min-h-screen bg-gradient-to-br from-slate-900 via-black to-slate-800 flex items-center justify-center p-4">
<div className="text-center">
+17 -56
View File
@@ -21,66 +21,27 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const [loading, setLoading] = useState(true);
useEffect(() => {
// Register service worker for FCM
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/firebase-messaging-sw.js')
.then(() => {
console.log('Service Worker registered for FCM');
})
.catch((error) => {
console.error('Service Worker registration failed:', error);
});
}
const initMessaging = async () => {
const { getMessaging, getToken, onMessage } = await import('firebase/messaging');
const messaging = getMessaging(app);
const unsubscribe = onAuthStateChanged(auth, async (user) => {
setUser(user);
if (user) {
// Request FCM token and save to Firestore only if permission granted
if (Notification.permission === 'granted') {
try {
const token = await getToken(messaging, { vapidKey: 'YOUR_VAPID_KEY' }); // Replace with actual VAPID key
if (token) {
await setDoc(doc(db, 'users', user.uid), { fcmToken: token }, { merge: true });
}
} catch (error) {
console.error('Error getting FCM token:', error);
}
}
// Handle foreground messages
onMessage(messaging, (payload) => {
console.log('Message received. ', payload);
// Show notification or alert
if (Notification.permission === 'granted') {
new Notification(payload.notification?.title || 'Prayer Reminder', {
body: payload.notification?.body,
icon: '/icon-192x192.png'
});
}
});
}
setLoading(false);
});
return unsubscribe;
};
let unsubscribe: (() => void) | undefined;
if (typeof window !== 'undefined') {
initMessaging().then((unsub) => unsubscribe = unsub);
} else {
const unsub = onAuthStateChanged(auth, (user) => {
// Simple auth state listener - no messaging complexity
let mounted = true;
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (mounted) {
setUser(user);
setLoading(false);
});
unsubscribe = unsub;
}
}
});
// Timeout safety: force loading to false after 1 second
const timeout = setTimeout(() => {
if (mounted) {
setLoading(false);
}
}, 1000);
return () => {
if (unsubscribe) unsubscribe();
mounted = false;
unsubscribe();
clearTimeout(timeout);
};
}, []);