Added token expired handling to mercure binding

This commit is contained in:
Alejandro Celaya
2020-04-18 12:25:47 +02:00
parent ed40b79c8d
commit a485d0b507
5 changed files with 13 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
import { EventSourcePolyfill as EventSource } from 'event-source-polyfill';
export const bindToMercureTopic = (mercureInfo, topic, onMessage) => () => {
export const bindToMercureTopic = (mercureInfo, topic, onMessage, onTokenExpired) => () => {
const { mercureHubUrl, token, loading, error } = mercureInfo;
if (loading || error) {
@@ -17,9 +17,7 @@ export const bindToMercureTopic = (mercureInfo, topic, onMessage) => () => {
});
es.onmessage = ({ data }) => onMessage(JSON.parse(data));
// TODO Handle errors and get a new token
es.onerror = () => {};
es.onerror = ({ status }) => status === 401 && onTokenExpired();
return () => es.close();
};