Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subscription UI was broken when using an Authorization header #327

Merged
merged 1 commit into from Jun 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 34 additions & 28 deletions public/app.js
Expand Up @@ -206,41 +206,47 @@ foo`;
subscriptionEventSource && subscriptionEventSource.close();
$subscriptions.textContent = "";

const opt =
$settingsForm.authorization.value === "header"
? { Authorization: `Bearer ${$settingsForm.jwt.value}` }
: undefined;
const resp = await fetch(
`${$settingsForm.hubUrl.value}/subscriptions`,
opt
);
const json = await resp.json();
try {
const opt =
$settingsForm.authorization.value === "header"
? { headers: { Authorization: `Bearer ${$settingsForm.jwt.value}` } }
: undefined;
const resp = await fetch(
`${$settingsForm.hubUrl.value}/subscriptions`,
opt
);
if (!resp.ok) throw new Error(resp.statusText);
const json = await resp.json();

json.subscriptions.forEach(addSubscription);
json.subscriptions.forEach(addSubscription);

const u = new URL($settingsForm.hubUrl.value);
u.searchParams.append(
"topic",
"/.well-known/mercure/subscriptions{/topic}{/subscriber}"
);
u.searchParams.append("Last-Event-ID", json.lastEventID);
const u = new URL($settingsForm.hubUrl.value);
u.searchParams.append(
"topic",
"/.well-known/mercure/subscriptions{/topic}{/subscriber}"
);
u.searchParams.append("Last-Event-ID", json.lastEventID);

if (opt) subscriptionEventSource = new EventSourcePolyfill(u, opt);
else subscriptionEventSource = new EventSource(u);
if (opt) subscriptionEventSource = new EventSourcePolyfill(u, opt);
else subscriptionEventSource = new EventSource(u);

subscriptionEventSource.onmessage = function (e) {
const s = JSON.parse(e.data);
subscriptionEventSource.onmessage = function (e) {
const s = JSON.parse(e.data);

if (s.active) {
addSubscription(s);
return;
}
if (s.active) {
addSubscription(s);
return;
}

document.getElementById(s.id).remove();
};
subscriptionEventSource.onerror = console.log;
document.getElementById(s.id).remove();
};
subscriptionEventSource.onerror = console.log;

$subscriptionsForm.elements.unsubscribe.disabled = false;
$subscriptionsForm.elements.unsubscribe.disabled = false;
} catch (e) {
error(e);
return;
}
};
$subscriptionsForm.elements.unsubscribe.onclick = function (e) {
e.preventDefault();
Expand Down