summaryrefslogtreecommitdiff
path: root/ui/routes/+layout.svelte
diff options
context:
space:
mode:
Diffstat (limited to 'ui/routes/+layout.svelte')
-rw-r--r--ui/routes/+layout.svelte26
1 files changed, 16 insertions, 10 deletions
diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte
index 9089c7e..841d597 100644
--- a/ui/routes/+layout.svelte
+++ b/ui/routes/+layout.svelte
@@ -10,7 +10,7 @@
function doSubscribe() {
navigator.serviceWorker.ready
- .then(async(registration) => {
+ .then(async (registration) => {
const response = await fetch("/api/vapid");
// and if we fail to get it?
const vapidPublicKey = await response.text();
@@ -20,8 +20,13 @@
applicationServerKey: convertedVapidKey
});
}).then((subscription) => {
- subscriptionJson = JSON.parse(JSON.stringify(subscription));
- return fetch("/api/register", {
+ const subJson = subscription.toJSON();
+ subscriptionJson = {
+ endpoint: subJson.endpoint,
+ p256dh: subJson.keys.p256dh,
+ auth: subJson.keys.auth,
+ };
+ return fetch("/api/push", {
method: "post",
headers: { "Content-type": "application/json" },
body: JSON.stringify(subscriptionJson),
@@ -34,14 +39,15 @@
.then((registration) => {
return registration.pushManager.getSubscription();
}).then((subscription) => {
+ const { endpoint } = subscription.toJSON();
return subscription.unsubscribe()
- .then(function() {
- subscriptionJson = null;
- return fetch("/api/unregister", {
- method: "post",
+ .then(() => {
+ fetch("/api/push", {
+ method: "delete",
headers: { "Content-type": "application/json" },
- body: JSON.stringify({ subscription })
+ body: JSON.stringify({ endpoint }),
});
+ subscriptionJson = null;
});
});
}
@@ -59,8 +65,8 @@
method: "post",
headers: { "Content-type": "application/json" },
body: JSON.stringify({
- ...subscriptionJson,
- msg: "oople doople",
+ endpoint: subscriptionJson.endpoint,
+ msg: JSON.stringify({"msg": "oople doople"}),
})
});
}