Skip to content

Web Push: implement push-subscription endpoints and Web Push delivery (VAPID) #386

Description

@zeemscript

Background

The frontend has a Web Push client (lib/push/pushManager.js) that, on enabling notifications, subscribes the browser and POSTs the subscription to POST /api/users/push-subscription. That endpoint does not exist, and the backend has no Web Push delivery at all — only a notifications.push preference boolean. So push notifications cannot work end-to-end. This issue implements the backend half. (Paired with the dnb-frontend config/UX issue.)

Scope

  1. Dependency + config: add web-push; introduce VAPID_PUBLIC_KEY, VAPID_PRIVATE_KEY, VAPID_SUBJECT env vars (document in .env.example; generate with npx web-push generate-vapid-keys). The public key must equal the frontend NEXT_PUBLIC_VAPID_PUBLIC_KEY.
  2. Persist subscriptions per user (User model field or a PushSubscription collection keyed by user): store endpoint, keys.{p256dh,auth}, optional userAgent, createdAt. One row per unique endpoint; upsert.
  3. Endpoints (authenticated, mounted under /api/users):
    • POST /push-subscription — upsert the caller's subscription.
    • DELETE /push-subscription — remove by endpoint.
    • PATCH /push-subscription/preferences — update the user's push preference.
  4. Delivery: when a notification is created (integrate in src/controllers/notificationController.js / the notification-send path), send a Web Push to each of the user's subscriptions via web-push, gated on the notifications.push preference. On a 404/410 from the push service, prune that dead subscription.
  5. Tests: subscription upsert/delete, preference PATCH, delivery (mock web-push), preference gating, and dead-subscription pruning.

Out of scope

  • The frontend config/UX (separate dnb-frontend issue).
  • The separate Firebase FCM path — do not build on it here.

Acceptance criteria

  • web-push added; VAPID envs documented in .env.example
  • Subscriptions persisted per user (upsert by endpoint)
  • POST/DELETE /api/users/push-subscription and PATCH /api/users/push-subscription/preferences implemented and authenticated
  • Notifications deliver a Web Push to the user's subscriptions, gated on the push preference
  • Dead subscriptions (404/410) pruned
  • Tests cover CRUD, delivery, gating, pruning; suite + lint green
  • PR targets dev; no unrelated changes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions