fix: wait for hydration before checking auth in NotificationHandler
All checks were successful
Build frontend / build (push) Successful in 51s
All checks were successful
Build frontend / build (push) Successful in 51s
This commit is contained in:
@ -1,33 +1,44 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState, useRef } from "react";
|
||||||
import { requestNotificationPermission, onForegroundMessage } from "../utils/firebase";
|
import { requestNotificationPermission, onForegroundMessage } from "../utils/firebase";
|
||||||
import AuthService from "../services/AuthService";
|
import AuthService from "../services/AuthService";
|
||||||
|
|
||||||
export default function NotificationHandler() {
|
export default function NotificationHandler() {
|
||||||
const [notification, setNotification] = useState(null);
|
const [notification, setNotification] = useState(null);
|
||||||
const [unsubscribe, setUnsubscribe] = useState(null);
|
const initialized = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const user = AuthService.getUser();
|
// Wait for hydration to complete
|
||||||
if (!user) return;
|
const timer = setTimeout(() => {
|
||||||
|
if (initialized.current) return;
|
||||||
|
initialized.current = true;
|
||||||
|
|
||||||
// User is signed in — request permission and listen for messages
|
const token = AuthService.getToken();
|
||||||
requestNotificationPermission().then((token) => {
|
if (!token) {
|
||||||
if (token) {
|
console.log("[Notifications] Not signed in, skipping");
|
||||||
console.log("[Notifications] FCM token obtained");
|
return;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const unsub = onForegroundMessage((payload) => {
|
console.log("[Notifications] Signed in, requesting permission...");
|
||||||
const title = payload.notification?.title || payload.data?.title || "Sweet Home";
|
requestNotificationPermission().then((fcmToken) => {
|
||||||
const body = payload.notification?.body || payload.data?.body || "";
|
if (fcmToken) {
|
||||||
setNotification({ title, body });
|
console.log("[Notifications] FCM token obtained:", fcmToken.substring(0, 20) + "...");
|
||||||
setTimeout(() => setNotification(null), 5000);
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
setUnsubscribe(() => unsub);
|
const unsub = onForegroundMessage((payload) => {
|
||||||
return () => unsub();
|
const title = payload.notification?.title || payload.data?.title || "Sweet Home";
|
||||||
|
const body = payload.notification?.body || payload.data?.body || "";
|
||||||
|
setNotification({ title, body });
|
||||||
|
setTimeout(() => setNotification(null), 5000);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cleanup on unmount
|
||||||
|
return () => unsub();
|
||||||
|
}, 1000); // Wait 1s for hydration
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!notification) return null;
|
if (!notification) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user