Skip to content

Commit

Permalink
docs: fixed parsing of base64String by regex, updated subscribeToPush…
Browse files Browse the repository at this point in the history
… function

Change 1:
At line 107, the regex code is now fixed to replace all "-" with "+", which wasn't as expected with previous code, window.atob() would throw an error saying the string is not encoded properly. The variable base64 will now have a URL safe base64String.

Change 2:
At line 177 and 178, the function is now updated to call the server action subscribeUser() with a serialized object instead of a PushSubscription object, as server actions can only be called with serialized objects.

Thankyou.
  • Loading branch information
abyii authored Dec 14, 2024
1 parent a0260b3 commit 4ef5c26
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import { subscribeUser, unsubscribeUser, sendNotification } from './actions'
function urlBase64ToUint8Array(base64String: string) {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4)
const base64 = (base64String + padding)
.replace(/\\-/g, '+')
.replace(/-/g, '+')
.replace(/_/g, '/')

const rawData = window.atob(base64)
Expand Down Expand Up @@ -174,7 +174,8 @@ function PushNotificationManager() {
),
})
setSubscription(sub)
await subscribeUser(sub)
const serializedSub = JSON.parse(JSON.stringify(sub))
await subscribeUser(serializedSub)
}

async function unsubscribeFromPush() {
Expand Down

0 comments on commit 4ef5c26

Please sign in to comment.