Skip to content

Commit

Permalink
added secure ssl condition
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Apr 18, 2024
1 parent 33b3653 commit 03db686
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
84 changes: 43 additions & 41 deletions app/packs/entrypoints/controllers/push_notification_controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static values = {
static values = {
vapidPublic: String,
userLogin: Boolean
};

connect() {
if(!this.userLoginValue) return;
if (!this.userLoginValue) return;

let subscribed = localStorage.getItem('block-webpush-modal');
if (subscribed === 'true') {
Expand All @@ -25,8 +25,10 @@ export default class extends Controller {
}
}
}
)}
)}
)
}
)
}
}

requestPermission() {
Expand All @@ -42,41 +44,41 @@ export default class extends Controller {
setupPushNotifications() {
const applicationServerKey = this.urlBase64ToUint8Array(this.vapidPublicValue);

console.log(this.vapidPublicValue);

navigator.serviceWorker.register("/service-worker.js", {scope: "./" }).then((registration) => {
console.log('Service Worker registered successfully:', registration);

registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then((subscription) => {

const endpoint = subscription.endpoint;
const p256dh = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh'))));
const auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth'))));

fetch('/push_subscriptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({
subscription: {
endpoint: endpoint,
p256dh: p256dh,
auth: auth
}
})
if (window.isSecureContext) {
navigator.serviceWorker.register("/service-worker.js", { scope: "./" }).then((registration) => {
console.log('Service Worker registered successfully:', registration);

registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then((subscription) => {

const endpoint = subscription.endpoint;
const p256dh = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh'))));
const auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth'))));

fetch('/push_subscriptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({
subscription: {
endpoint: endpoint,
p256dh: p256dh,
auth: auth
}
})
});

localStorage.setItem('block-webpush-modal', 'true');
document.querySelector('.webpush-modal').style.display = 'none'
});

localStorage.setItem('block-webpush-modal', 'true');
document.querySelector('.webpush-modal').style.display = 'none'
});
}).catch(err => {
console.log('Service Worker registration failed:', err);
});;
}).catch(err => {
console.log('Service Worker registration failed:', err);
});;
}
}

close() {
Expand All @@ -91,14 +93,14 @@ export default class extends Controller {
urlBase64ToUint8Array(base64String) {
var padding = '='.repeat((4 - base64String.length % 4) % 4);
var base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
.replace(/\-/g, '+')
.replace(/_/g, '/');

var rawData = window.atob(base64);
var outputArray = new Uint8Array(rawData.length);

for (var i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
outputArray[i] = rawData.charCodeAt(i);
}

return outputArray;
Expand Down
2 changes: 1 addition & 1 deletion app/packs/entrypoints/controllers/wishlist_controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller } from "@hotwired/stimulus"
import Rails from 'rails-ujs';
// import Rails from 'rails-ujs';

export default class extends Controller {
connect() {
Expand Down

0 comments on commit 03db686

Please sign in to comment.