← Public packages
@kentcdodds/pie-night
Pie night sign-ups: hosts start an event, guests add the pies they are bringing and watch the list fill in live. Installable, with optional push notifications.
Files
/**
* Set the public half of the VAPID key. First save its private half as a Kody
* signing credential (provider web-push), then map the webPush slot to it.
* This export never takes or returns a private key.
*/
import { b64urlDecode } from './push-crypto.ts'
import { getPushPublicKey, setPushPublicKey } from './store.ts'
export default async function setupPush(params: { publicKey?: string } = {}) {
if (!params.publicKey) return { ok: true, publicKey: await getPushPublicKey(), message: 'Save a VAPID signing credential outside package code, map webPush, then pass its publicKey here.' }
const key = String(params.publicKey).trim()
let bytes: Uint8Array
try { bytes = b64urlDecode(key) } catch { bytes = new Uint8Array() }
if (bytes.length !== 65 || bytes[0] !== 4) return { ok: false, publicKey: '', message: 'That is not a P-256 public key (65 bytes, base64url).' }
await setPushPublicKey(key)
return { ok: true, publicKey: key, message: 'Saved the public key. Notifications turn on once webPush is mapped.' }
}