source: Klonkt/src/routes/admin-push.js@ 053bf51

main
Last change on this file since 053bf51 was 053bf51, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: web push slice 2, enable/disable UI + service worker delivery

The visible half of docs/webpush-design.md: an owner can now turn on
notifications per device, pick what to be notified about, and send a test.

  • Routes: GET /push/vapid (the public key, public by design), POST /push/subscribe|/unsubscribe|/alerts|/test (logged-in; a subscription row is personal, only its creator may touch it). Mounted before the /:slug catch-all, like /paid.
  • Service worker: push handler (shows the encrypted JSON payload {type,title,body,url}; same-type bursts collapse via tag) and notificationclick (focus an open tab and navigate, else open a window). Cache name bumped to v19.
  • Beheer -> Notificaties (/admin/push): per-device toggle, alert-type checkboxes (saved prefs shown for the current device), test button, linked-devices list with remove, iOS install hint (push needs an installed PWA there), plain <script> so injectCspNonce provides the real nonce.
  • Not premium-gated: notifications are infrastructure, not an extra.

Verified live on a dev server: /push/vapid serves the generated key,
storage/.vapid is 0600, sw.js carries both handlers, and an unauthenticated
subscribe is refused.

Changed files:
src/server.js

  • mount /push + /admin/push; sw.js push/notificationclick handlers, v19

src/views/pages/admin.ejs

  • "Notificaties" button (always visible, not premium)

New file:
src/routes/push.js

  • vapid/subscribe/unsubscribe/alerts/test

src/routes/admin-push.js

  • the Beheer page (requireSiteManager)

src/views/pages/admin-push.ejs

  • device toggle, prefs, test, device list, iOS hint

-robo
Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 757 bytes
Line 
1/**
2 * Admin: Notificaties (web push) — the owner's per-device toggle page.
3 * Not premium-gated: notifications are infrastructure, not an extra.
4 */
5import express from 'express';
6import { renderPage } from '../middleware/render.js';
7import { requireSiteManager } from '../middleware/auth.js';
8import Push from '../services/PushService.js';
9
10const router = express.Router();
11
12router.get('/', requireSiteManager, async (req, res) => {
13 renderPage(req, res, 'pages/admin-push', {
14 pageTitle: 'Notificaties',
15 bodyClass: 'on-admin',
16 vapidKey: await Push.publicKey(), // null → feature unavailable
17 subscriptions: Push.listSubscriptions(req.session.user.id),
18 defaultAlerts: Push.DEFAULT_ALERTS,
19 });
20});
21
22export default router;
Note: See TracBrowser for help on using the repository browser.