Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Web Push: where can I get "auth" key?

I'm starting to use Web Push Notification; I don't know where I can find the auth key:

var pushSubscription = {
  endpoint: '< Push Subscription URL >',
  keys: {
    p256dh: '< User Public Encryption Key >',
    auth: '< ???? User Auth Secret ???? >'
  }
};

I can get endpoint and p256dh from ServiceWorker>registeration.pushManager.getSubscription() but not the auth key.

Thanks

like image 721
Ali Amini Avatar asked Oct 16 '25 01:10

Ali Amini


2 Answers

You can use the getKey method to get both p256dh and auth (see the specs or the example from the specs).

It's even simpler to just call JSON.stringify on the PushSubscription object returned by the getSubscription promise.

like image 99
Marco Castelluccio Avatar answered Oct 17 '25 14:10

Marco Castelluccio


Using Typescript, the PushSubscription object should have a method on it called toJSON. Just use that.

const sub: PushSubscription = YOUR_RAW_PUSH_SUBSCRIPTION;
const pushSubscription = {
  endpoint: sub.endpoint,
  expirationTime: sub.expirationTime,
  keys: {
    p256dh: sub.toJSON().keys.p256dh,
    auth: sub.toJSON().keys.auth
  }
};
like image 45
Greg Avatar answered Oct 17 '25 13:10

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!