Reputation: 1
I am using server-sent events in my React project. While it is working on Postman, I am receiving 401 unauthorized on frontend implementation.
useEffect(() => {
const token = localStorage.getItem("authToken");
const evtSource = new EventSource(
`http://localhost:5001/scienceacademyapi/v1/notification/stream?token=${token}`
);
evtSource.onmessage = (event) => {
const newNotification = JSON.parse(event.data);
setShowNotifications((prev) => [...prev, newNotification]);
};
evtSource.onerror = (err) => {
console.error("Error with sse : ", err);
evtSource.close();
};
return () => {
evtSource.close();
};
}, []);
Upvotes: 0
Views: 22